MySQL结合外连接

时间:2016-02-10 07:22:10

标签: mysql join

对于这个问题,我创建了一个简单的例子来说明我的要求。

说我有一张名为' books'

的桌子
+----+----------------------------+-----------+
| pk |           title            | author_id |
+----+----------------------------+-----------+
|  1 | The Lost Symbol            |         1 |
|  2 | Follow Us Home             |         2 |
|  3 | The Man in the High Castle |         3 |
+----+----------------------------+-----------+
                (table a)

还有另一张叫做“商店”的桌子,里面有一套卖掉每本书的商店:

+----+---------+-------------+-------+
| pk | book_id |  shop_name  | price |
+----+---------+-------------+-------+
|  1 |       1 | WHSmith     |  5.00 |
|  2 |       1 | Waterstones |  7.00 |
|  3 |       1 | Amazon      |  2.50 |
|  4 |       2 | WHSmith     |  4.00 |
|  5 |       2 | Borders     |  4.50 |
+----+---------+-------------+-------+
                (table b)

如果我做一个简单的select抓取一本书以及使用join出售的所有地方,例如:

SELECT 
    books.*,
    shops.shop_name,
    shops.price
FROM 
    books
    JOIN shops ON books.pk = shops.book_id
WHERE 
    book.book_name = "The Lost Symbol"

我会得到如下结果:

+----+-----------------+-----------+-------------+-------+
| pk |      title      | author_id |  shop_name  | price |
+----+-----------------+-----------+-------------+-------+
|  1 | The Lost Symbol |         1 | WHSmith     |  5.00 |
|  1 | The Lost Symbol |         1 | Waterstones |  7.00 |
|  1 | The Lost Symbol |         1 | Amazon      |  2.50 |
+----+-----------------+-----------+-------------+-------+
                      (table c)

但是,我希望收到这样的结果:

+----+-----------------+-----------+-------------+-------+
| pk |      title      | author_id |  shop_name  | price |
+----+-----------------+-----------+-------------+-------+
|  1 | The Lost Symbol |         1 | NULL        |  NULL |
|  1 | The Lost Symbol |         1 | WHSmith     |  5.00 |
|  1 | The Lost Symbol |         1 | Waterstones |  7.00 |
|  1 | The Lost Symbol |         1 | Amazon      |  2.50 |
+----+-----------------+-----------+-------------+-------+
                        (table d)

即。第一行只是左外连接的结果,其余结果是内连接。

更理想的结果是:

+------+-----------------+-----------+-------------+-------+
|  pk  |      title      | author_id |  shop_name  | price |
+------+-----------------+-----------+-------------+-------+
|    1 | The Lost Symbol |         1 | NULL        |  NULL |
| NULL |            NULL |      NULL | WHSmith     |  5.00 |
| NULL |            NULL |      NULL | Waterstones |  7.00 |
| NULL |            NULL |      NULL | Amazon      |  2.50 |
+------+-----------------+-----------+-------------+-------+
                          (table e)

shop_nameprice连接并分组在一行似乎不起作用,因为它只会从shops而不是所有这些结果中得到第一个结果,在我的现实世界中也是如此方案,我在数据中有标点符号,所以必须小心分隔符。

那么我如何获得表e的结果?

1 个答案:

答案 0 :(得分:1)

您可以使用UNION ALL构建所需的结果集:

SELECT pk, title, author_id, NULL AS shop_name, NULL AS price
FROM books
WHERE books.title = "The Lost Symbol"

UNION ALL

SELECT NULL AS pk, NULL AS title, NULL AS author_id, shops.shop_name, shops.price
FROM books
JOIN shops ON books.pk = shops.book_id
WHERE books.title = "The Lost Symbol"

union操作的第一部分返回结果的第一行,即书名。第二部分返回其余行,即商店名称。

Demo here