原始数据:
UNION ALL 行转列后:
SELECT userid,'语文' AS course,cn_score AS score FROM tb_score1
UNION ALL
SELECT userid,'数学' AS course,math_score AS score FROM tb_score1
UNION ALL
SELECT userid,'英语' AS course,en_score AS score FROM tb_score1
UNION ALL
SELECT userid,'政治' AS course,po_score AS score FROM tb_score1
ORDER BY userid
区别:
1.对重复结果的处理:UNION会去掉重复记录,UNION ALL不会
2.对排序的处理:UNION会排序,UNION ALL只是简单地将两个结果集合并
3.效率方面的区别:因为UNION 会做去重和排序处理,因此效率比UNION ALL慢很多