+----------------------+
| 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_replies
的post_id
表post_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
这将仅从表返回消息
有可能吗?
非常感谢任何帮助