在条件下执行多个左连接并获取非空列值
Do Multiple Left Join on condition and get non null column value
我有一个 table 包含网站上发布的最后评论,我想根据评论类型加入不同的 table。
评论Table类似这样的结构:
id | type | ressource_id |
---+------+--------------+
1 | 1 | 10 |
2 | 3 | 7 |
3 | 3 | 12 |
4 | 1 | 22 |
5 | 4 | 22 |
6 | 5 | 23 |
新闻Table:
news_id | notes| date |
--------+------+--------------+
10 | | 2015-08-12 |
22 | | 2015-07-12 |
教程Table:
tuto_id | notes| date |
--------+------+--------------+
7 | | 2015-06-15 |
12 | | 2015-05-14 |
... 类似 table for type = 4, 5, 6
现在为了获得具体评论,我正在对两个 table 进行左连接。
SELECT co.*
FROM Comments co
LEFT JOIN News n
ON co.id = n.news_id AND co.type = 1
LEFT JOIN Tutorial t
ON co.id = t.tuto_id AND co.type = 3
WHERE (co.type IN (1,3))
我有兴趣从左边获取日期 table。如何将该列包含在输出列表中。
期望的结果:(加入日期 table)
id | type | ressource_id | date |
---+------+--------------+--------------+
1 | 1 | 10 | 2015-08-12 |
2 | 3 | 7 | 2015-06-15 |
3 | 3 | 12 | 2015-05-14 |
4 | 1 | 22 | 2015-07-12 |
谢谢。
因为您永远不会从 News
和 Tutorial
获得同一个 comment you might go with
COALESCE` 的日期:
SELECT co.*, COALESCE(n.date,t.date)
FROM Comments co
LEFT JOIN News n
ON co.id = n.news_id AND co.type = 1
LEFT JOIN Tutorial t
ON co.id = t.tuto_id AND co.type = 3
WHERE (co.type IN (1,3))
COALESCE
将 return 不是 null
的第一个参数,因此如果有匹配的新闻,它将 return 来自 News
的日期如果没有匹配的新闻但有匹配的教程,它将 return 来自 Tutorial
的日期。
试试这个:
SELECT co.*,coalesce(n.date,t.date)
FROM Comments co
LEFT JOIN News n
ON co.id = n.news_id AND co.type = 1
LEFT JOIN Tutorial t
ON co.id = t.tuto_id AND co.type = 3
WHERE (co.type IN (1,3))
and (co.id = n.news_id or co.id = t.tuto_id)
您也可以使用 UNION 运算符。
(SELECT co.*,n.date as [date]
FROM Comments co
LEFT JOIN News n
ON co.id = n.news_id AND co.type = 1)
UNION ALL
(SELECT co.*,t.date
FROM Comments co
LEFT JOIN Tutorial t
ON co.id = t.tuto_id AND co.type = 3)
ORDER BY ID
我有一个 table 包含网站上发布的最后评论,我想根据评论类型加入不同的 table。
评论Table类似这样的结构:
id | type | ressource_id |
---+------+--------------+
1 | 1 | 10 |
2 | 3 | 7 |
3 | 3 | 12 |
4 | 1 | 22 |
5 | 4 | 22 |
6 | 5 | 23 |
新闻Table:
news_id | notes| date |
--------+------+--------------+
10 | | 2015-08-12 |
22 | | 2015-07-12 |
教程Table:
tuto_id | notes| date |
--------+------+--------------+
7 | | 2015-06-15 |
12 | | 2015-05-14 |
... 类似 table for type = 4, 5, 6
现在为了获得具体评论,我正在对两个 table 进行左连接。
SELECT co.*
FROM Comments co
LEFT JOIN News n
ON co.id = n.news_id AND co.type = 1
LEFT JOIN Tutorial t
ON co.id = t.tuto_id AND co.type = 3
WHERE (co.type IN (1,3))
我有兴趣从左边获取日期 table。如何将该列包含在输出列表中。
期望的结果:(加入日期 table)
id | type | ressource_id | date |
---+------+--------------+--------------+
1 | 1 | 10 | 2015-08-12 |
2 | 3 | 7 | 2015-06-15 |
3 | 3 | 12 | 2015-05-14 |
4 | 1 | 22 | 2015-07-12 |
谢谢。
因为您永远不会从 News
和 Tutorial
获得同一个 comment you might go with
COALESCE` 的日期:
SELECT co.*, COALESCE(n.date,t.date)
FROM Comments co
LEFT JOIN News n
ON co.id = n.news_id AND co.type = 1
LEFT JOIN Tutorial t
ON co.id = t.tuto_id AND co.type = 3
WHERE (co.type IN (1,3))
COALESCE
将 return 不是 null
的第一个参数,因此如果有匹配的新闻,它将 return 来自 News
的日期如果没有匹配的新闻但有匹配的教程,它将 return 来自 Tutorial
的日期。
试试这个:
SELECT co.*,coalesce(n.date,t.date)
FROM Comments co
LEFT JOIN News n
ON co.id = n.news_id AND co.type = 1
LEFT JOIN Tutorial t
ON co.id = t.tuto_id AND co.type = 3
WHERE (co.type IN (1,3))
and (co.id = n.news_id or co.id = t.tuto_id)
您也可以使用 UNION 运算符。
(SELECT co.*,n.date as [date]
FROM Comments co
LEFT JOIN News n
ON co.id = n.news_id AND co.type = 1)
UNION ALL
(SELECT co.*,t.date
FROM Comments co
LEFT JOIN Tutorial t
ON co.id = t.tuto_id AND co.type = 3)
ORDER BY ID