小编典典

JOIN 和 UNION 有什么区别?

all

JOIN和 和有什么不一样UNION?我可以举个例子吗?


阅读 74

收藏
2022-04-22

共1个答案

小编典典

UNION将查询中的行放在一起,同时JOIN制作笛卡尔积并将其子集——完全不同的操作。的简单示例UNION

mysql> SELECT 23 AS bah
    -> UNION
    -> SELECT 45 AS bah;
+-----+
| bah |
+-----+
|  23 | 
|  45 | 
+-----+
2 rows in set (0.00 sec)

类似的简单示例JOIN

mysql> SELECT * FROM 
    -> (SELECT 23 AS bah) AS foo 
    -> JOIN 
    -> (SELECT 45 AS bah) AS bar
    -> ON (33=33);
+-----+-----+
| foo | bar |
+-----+-----+
|  23 |  45 | 
+-----+-----+
1 row in set (0.01 sec)
2022-04-22