我有这两个表:
// posts
+----+---------+------------------------+-----------+------------+
| id | title | content | author_id | visibility |
+----+---------+------------------------+-----------+------------+
| 1 | title1 | content1 | 3 | 1 |
| 2 | title2 | content2 | 2 | 0 |
+----+---------+------------------------+-----------+------------+
// users
+----+---------+----------+
| id | fname | lname |
+----+---------+----------+
| 1 | John | Tersco |
| 2 | Peter | Brlew |
| 3 | Jack | Keveinc |
+----+---------+----------+
我希望这是预期输出:
+----+---------+------------------------+--------------+
| id | title | content | full_name |
+----+---------+------------------------+--------------+
| 1 | title1 | content1 | Jack Keveinc |
| 2 | title2 | content2 | Unknown |
+----+---------+------------------------+--------------+
这是我当前的查询:
SELECT p.id, p.title, p.content, CONCAT(u.fname, ' ', u.lname) as full_name
FROM posts p
LEFT JOIN users u
ON p.author_id = u.id AND p.visibility = 1
如您所见,我需要的是在Unknown
等于p.visibility
时定义0
。我怎么能这样做?
我是否需要CASE
或COALESCE
或两者?
编辑:首先,我要说Tim Biegeleisen的答案很棒,一切都很好。实际上,还有另一个专栏,我需要用它完全像上面那样对待它。但它没有CONCAT
功能。例如:
// users
+----+---------+----------+------------+
| id | fname | lname | reputation |
+----+---------+----------+------------+
| 1 | John | Tersco | 943 |
| 2 | Peter | Brlew | 2300 |
| 3 | Jack | Keveinc | 84 |
+----+---------+----------+------------+
这是预期结果:
+----+---------+------------------------+--------------+------------+
| id | title | content | full_name | reputation |
+----+---------+------------------------+--------------+------------+
| 1 | title1 | content1 | Jack Keveinc | 84 |
| 2 | title2 | content2 | Unknown | N |
+----+---------+------------------------+--------------+------------+
好的,我想知道的是:我还可以将COALESCE
用于reputation
列吗? (它没有CONCAT
)
SELECT COALESECE(reputation, 'N') reputation, ....
没事吗?或者因为它没有CONCAT
,我必须使用CASE
吗?
答案 0 :(得分:3)
我认为下面的查询是最好的方法,因为如果以下条件的 ,它将显示Unknown
full_name
:
visibility = 0
)
使用您最初拥有的相同加入条件,在这两种情况下,加入的users
表的名字和姓氏都是NULL
,我们可以显示Unknown
。
SELECT p.id,
p.title,
p.content,
COALESECE(CONCAT(u.fname, ' ', u.lname), 'Unknown') AS full_name
FROM posts p
LEFT JOIN users u
ON p.author_id = u.id AND
p.visibility = 1
<强>更新强>
在连接的名字和姓氏上使用COALESCE
就足够了,因为如果任何参数为CONCAT
NULL
,则NULL
会返回users
。documentation
修改强>
如果您的reputation
表格中有另一列名为COALESCE(u.reputation, 'N')
,那么您也可以使用users
。这样做的原因是来自NULL
表的所有列将posts
来自;with T as (
select event, rowindex, rowindex-row_number() over (order by event) as rn from YourTableName
)
select event, (select count(distinct rn)+1 from T where rn<TT.rn) as seq_id
from T TT order by event
的不匹配的记录。
答案 1 :(得分:1)
案件做的工作:
SELECT p.id, p.title, p.content, case p.visibility when 0 then 'unknown' else CONCAT(u.fname, ' ', u.lname) end as full_name
FROM posts p
LEFT JOIN users u
ON p.author_id = u.id
答案 2 :(得分:1)
SELECT p.id
, p.title
, p.content
, CASE p.visibility
WHEN 0 THEN 'Unknown'
ELSE CONCAT(u.fname, ' ', u.lname)
END AS full_name
FROM posts p
LEFT JOIN users u
ON p.author_id = u.id AND p.visibility = 1
答案 3 :(得分:1)
您可以CONCAT(u.fname, ' ', u.lname)
替换CASE WHEN u.id IS NULL THEN 'unknown' ELSE CONCAT(u.fname, ' ', u.lname) END
。
答案 4 :(得分:1)
SELECT p.id, p.title, p.content, case p.visibility when 0 then 'Unknown' else CONCAT(u.fname, ' ', u.lname) end as full_name
FROM posts p
LEFT JOIN users u
ON p.author_id = u.id
答案 5 :(得分:1)
您必须在选择列时使用Case,
SELECT p.id, p.title, p.content,case when p.visibility=0 then 'Unknow'
else CONCAT(u.fname, ' ', u.lanme) end as full_name
FROM Posts p
JOIN users u
ON p.author_id = u.id