客户表:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
订单表:
+-----+---------------------+-------------+--------+
|OID | DATE | CUSTOMER_ID | AMOUNT |
+-----+---------------------+-------------+--------+
| 102 | 2009-10-08 00:00:00 | 3 | 3000 |
| 100 | 2009-10-08 00:00:00 | 3 | 1500 |
| 101 | 2009-11-20 00:00:00 | 2 | 1560 |
| 103 | 2008-05-20 00:00:00 | 4 | 2060 |
+-----+---------------------+-------------+--------+
查询:
SELECT ID, NAME, AMOUNT, DATE
FROM CUSTOMERS
FULL OUTER JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
根据TutorialsPoint,输出应为:
+------+----------+--------+---------------------+
| ID | NAME | AMOUNT | DATE |
+------+----------+--------+---------------------+
| 1 | Ramesh | NULL | NULL |
| 2 | Khilan | 1560 | 2009-11-20 00:00:00 |
| 3 | kaushik | 3000 | 2009-10-08 00:00:00 |
| 3 | kaushik | 1500 | 2009-10-08 00:00:00 |
| 4 | Chaitali | 2060 | 2008-05-20 00:00:00 |
| 5 | Hardik | NULL | NULL |
| 6 | Komal | NULL | NULL |
| 7 | Muffy | NULL | NULL |
| 3 | kaushik | 3000 | 2009-10-08 00:00:00 |
| 3 | kaushik | 1500 | 2009-10-08 00:00:00 |
| 2 | Khilan | 1560 | 2009-11-20 00:00:00 |
| 4 | Chaitali | 2060 | 2008-05-20 00:00:00 |
+------+----------+--------+---------------------+
这是我的实际输出:
╔════╦══════════╦════════╦═════════════════════════╗
║ ID ║ NAME ║ AMOUNT ║ DATE ║
╠════╬══════════╬════════╬═════════════════════════╣
║ 1 ║ Ramesh ║ NULL ║ NULL ║
║ 2 ║ Khilan ║ 1560 ║ 2009-11-20 00:00:00.000 ║
║ 3 ║ kaushik ║ 1500 ║ 2009-10-08 00:00:00.000 ║
║ 3 ║ kaushik ║ 3000 ║ 2009-10-08 00:00:00.000 ║
║ 4 ║ Chaitali ║ 2060 ║ 2008-05-20 00:00:00.000 ║
║ 5 ║ Hardik ║ NULL ║ NULL ║
║ 6 ║ Komal ║ NULL ║ NULL ║
║ 7 ║ Muffy ║ NULL ║ NULL ║
╚════╩══════════╩════════╩═════════════════════════╝
所以,我想了解完全加入是如何工作的,因为我没有得到与Tutorilaspoint网站相同的结果。我还想知道为什么我在上面发布的完整联接查询将具有与LEFT JOIN完全相同的相同结果。
答案 0 :(得分:3)
您的结果是正确的。想想FULL OUTER JOIN
就像这样:
INNER JOIN
的所有行。在您的数据示例中,第二个表中的所有行都在第一个表中匹配(即Customer_Id
始终与有效客户匹配)。只有前两个要点描述了结果集。
我不知道他们的结果集来自哪里。我最好的猜测是它来自MySQL代码。该代码不正确,并且不以任何方式接近FULL OUTER JOIN
。
除了明确建议找到更好的教程外,这也是一个非常糟糕的例子。在正确创建的数据库中,几乎不需要FULL OUTER JOIN
。一个表中的键应与引用表中的键匹配。也就是说,你应该永远拥有在Customer_Id
表中无效的非NULL Customers
。