1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| # UNION联合可以使得多个结果合并到一起 SELECT o.`order_id`, o.`order_date`, 'Active' AS STATUS FROM `orders` o WHERE `order_date` >='2019-01-01' UNION SELECT o.`order_id`, o.`order_date`, 'Archived' AS STATUS FROM `orders` o WHERE `order_date` <'2019-01-01';
# 他也可以联合不同的列到一起 SELECT `first_name` FROM `customers` UNION SELECT NAME FROM `shippers`;
# 练习: # 一万头曹尼玛 最后一行用c.`first_name`就不行,离谱 SELECT c.`customer_id`, c.`first_name`, c.`points`, 'Bronze' AS TYPE FROM `customers` c WHERE c.`points` < 2000 UNION SELECT c.`customer_id`, c.`first_name`, c.`points`, 'Silver' AS TYPE FROM `customers` c WHERE c.`points` BETWEEN 2000 AND 3000 UNION SELECT c.`customer_id`, c.`first_name`, c.`points`, 'Gold' AS TYPE FROM `customers` c WHERE c.`points` > 3000 ORDER BY `first_name`;
|