如何获得另一个帖子的标题?

时间:2018-01-30 06:55:43

标签: mysql sql join

我有一个Q& A网站,并且有一个名为questions_and_answers的表格。

我知道这个结构并不好(将问题和答案放在同一张表中),但我不想改变它。

表:questions_and_answers

+----+----------+-------------------+---------+---------+
| id |   title  |      content      | user_id | related |
+----+----------+-------------------+---------+---------+
| 1  | title1   | content1          | 105     | NULL    | -- related column is null for questions
| 2  |          | content2          | 56      | 1       | -- related column contains the id of its question
| 3  | title2   | content3          | 2235    | NULL    |
| 4  |          | content4          | 56      | 1       |
| 5  |          | content5          | 321     | 3       |
| 6  | title3   | content6          | 56      | NULL    |
| 7  | title4   | content7          | 874     | NULL    |
| 8  |          | content8          | 1001    | 6       |
+----+----------+-------------------+---------+---------+

我要做的就是选择特定用户的所有问题和答案。这是我的疑问:

SELECT * FROM questions_and_answers WHERE user_id = 56;

输出:

   +----+----------+-------------------+---------+---------+
    | id |   title  |      content      | user_id | related |
    +----+----------+-------------------+---------+---------+
    | 2  |          | content2          | 56      | 1       |
    | 4  |          | content4          | 56      | 1       |
    | 6  | title3   | content6          | 56      | NULL    |
    +----+----------+-------------------+---------+---------+

我的问题是,如何通过问题标题填写title列的答案?

这是预期结果

    +----+----------+-------------------+---------+---------+
    | id |   title  |      content      | user_id | related |
    +----+----------+-------------------+---------+---------+
    | 2  | title1   | content2          | 56      | 1       |
    | 4  | title1   | content4          | 56      | 1       |
    | 6  | title3   | content6          | 56      | NULL    |
    +----+----------+-------------------+---------+---------+

2 个答案:

答案 0 :(得分:3)

您可以执行以下LEFT JOINCASE声明:

SELECT qa.id
    ,CASE 
        WHEN qa1.title IS NULL
            THEN qa.title
        ELSE qa1.title
        END AS title
    ,qa.content
    ,qa.user_id
    ,qa.related
FROM questions_and_answers AS qa
LEFT JOIN questions_and_answers AS qa1 ON qa.related = qa1.id
WHERE qa.user_id = 56;

另外,使用'IFNULL'

SELECT qa.id
    ,IFNULL(qa1.Title,qa.Title) title
    ,qa.content
    ,qa.user_id
    ,qa.related
FROM questions_and_answers AS qa
LEFT JOIN questions_and_answers AS qa1 ON qa.related = qa1.id
WHERE qa.user_id = 56;

演示链接:http://sqlfiddle.com/#!9/5600e/24

答案 1 :(得分:1)

未经测试,但希望这个逻辑很好。

试试这个:

解决方案1:

SELECT T1.ID,CASE WHEN T1.Title = '' THEN T2.Title ELSE T1.Title END Title, T1.Content , T1.User_ID,T1.Related
FROM questions_and_answers T1
LEFT JOIN(
    SELECT A.content,B.ID,B.Title
    FROM questions_and_answers A
    JOIN questions_and_answers B ON A.Related=B.Id
    )T2 ON T1.content=T2.content
WHERE user_id = 56;

解决方案2:

而不是这个你可以使用LIKE

SELECT T1.ID,CASE WHEN T1.Title NOT LIKE '%title%' THEN T2.Title ELSE T1.Title END Title, T1.Content , T1.User_ID,T1.Related
FROM @questions_and_answers T1
LEFT JOIN @questions_and_answers T2 ON T1.Related=T2.Id
WHERE T1.user_id = 56;

<强>结果:

ID  Title   Content     User_ID Related
--  -----   --------    ------- -------
2   title1  content2    56      1
4   title1  content4    56      1
6   title3  content6    56      NULL