2
+----------------------+
| post_id | message    |
+----------------------+
| 1       | message 1  |
+----------------------+
| 2       | message 2  |
+----------------------+

+----------+---------+-------------------------+
| reply_id | post_id |message                  |
+----------+---------+-------------------------+
|    1     |    1    |  reply 1 to message 1   |
+----------+---------+-------------------------+
|    2     |    1    |  reply 2 to message 1   |
+----------+---------+-------------------------+
|    3     |    1    |  reply 3 to message 1   |
+----------+---------+-------------------------+
|    4     |    2    |  reply 1 to message 2   |
+----------+---------+-------------------------+

我得到了这两个表名post,并且表中post_repliespost_idpost_replies是 Fk 到表中的 Pkpost 我想要做的是找到发布消息,它只回复为一列

如果我运行这样的事情:

SELECT p.message, pr.message FROM `posts` AS p LEFT JOIN post_replies AS pr ON p.post_id = pr.post_id

它会返回这样的东西

+-----------+-------------------------+
| message   |   message               |
+-----------+-------------------------+
| message 1 |  reply 1 to message 1   |
+-----------+-------------------------+
| message 1 |  reply 2 to message 1   |
+-----------+-------------------------+
| message 1 |  reply 3 to message 1   |
+-----------+-------------------------+
| message 2 |  reply 1 to message 2   |
+-----------+-------------------------+

有没有办法我可以得到这样的结果

+-------------------------+
|   message               |
+-------------------------+
|   message 1             |
+-------------------------+
|  reply 1 to message 1   |
+-------------------------+
|  reply 2 to message 1   |
+-------------------------+
|  reply 3 to message 1   |
+-------------------------+
|   message 2             |
+-------------------------+
|  reply 1 to message 2   |
+-------------------------+

如果我跑

SELECT p.message FROM `posts` AS p LEFT JOIN post_replies AS pr ON p.id = pr.post_id

这将从post表中返回消息

或者

SELECT pr.message FROM `posts` AS p LEFT JOIN post_replies AS pr ON p.id = pr.post_id

post_replies这将仅从表返回消息

有可能吗?

非常感谢任何帮助

4

2 回答 2

6

将您提供的两个查询合并并引入正确的顺序:

SELECT * FROM (
  SELECT p.id, 0 x, p.message
  FROM posts AS p
  LEFT JOIN post_replies AS pr ON p.id = pr.post_id
  UNION ALL
  SELECT p.id, pr.id, pr.message
  FROM posts AS p
  LEFT JOIN post_replies AS pr ON p.id = pr.post_id
) x
ORDER BY 1, 2

如果您不想重复,请将“UNION ALL”更改为“UNION”。

于 2013-01-06T12:56:59.847 回答
2

我知道这不是你想要的,但如果这能解决你的问题就发帖

$查询

SELECT 
    group_concat(pr.message SEPARATOR '|'), p.message,  
FROM 
    `posts` AS p 
LEFT JOIN 
    post_replies AS pr ON p.post_id = pr.post_id 
GROUP BY
    pr.post_id


+-----------+----------------------------------------------------------------------------+
| message   |   message                                                                  |
+-----------+----------------------------------------------------------------------------+
| message 1 |  reply 1 to message 1 | reply 2 to message 1  | reply 3 to message 1       |
+-----------+----------------------------------------------------------------------------+
于 2013-01-06T12:57:01.550 回答