如何为每个帖子选择作者姓名?

时间:2016-09-15 06:14:15

标签: mysql sql

我有这两个表:

// 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。我怎么能这样做?

我是否需要CASECOALESCE或两者?

编辑:首先,我要说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吗?

6 个答案:

答案 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会返回usersdocumentation

修改

如果您的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