我有3张桌子
+----+---------+
| id | message |
+----+---------+
| 1 | sup |
+----+---------+
| 2 | hello |
+--------------+
+------------+----------+
| message_id | comments |
+------------+----------+
| 1 | hi |
+------------+----------+
| 1 | bye |
+------------+----------+
+------------+-------+
| message_id | likes |
+------------+-------+
| 1 | sup |
+------------+-------+
| 2 | hello |
+------------+-------+
我最终会找到像这样的东西
+----+---------+------------+----------+------------+-------+
| id | message | message_id | comments | message_id | likes |
+----+---------+------------+----------+------------+-------+
| 1 | sup | 1 | hi | null | null |
+----+---------+------------+----------+------------+-------+
| 1 | sup | 1 | bye | null | null |
+----+---------+------------+----------+------------+-------+
| 1 | hello | null | null | 1 | sup |
+----+---------+------------+----------+------------+-------+
| 1 | hello | null | null | 2 | hello |
+----+---------+------------+----------+------------+-------+
我有2个连接,比如
select * from message join comments on id = comments.message_id join likes on id = likes.message_id
但这会返回填充的所有字段。我希望字段不会在不存在的地方填充。这可能吗?
编辑:解决人们的意见
mysql> select * from message join comments on id = comments.message_id join likes on id = likes.message_id;
+----+---------+------------+----------------------+------------+-----------+
| id | message | message_id | comment | message_id | comment |
+----+---------+------------+----------------------+------------+-----------+
| 1 | hello | 1 | hello | 1 | what |
| 1 | hello | 1 | hello | 1 | what what |
| 2 | two | 2 | bye | 2 | what ok |
| 1 | hello | 1 | hello what what | 1 | what |
| 1 | hello | 1 | hello what what | 1 | what what |
| 1 | hello | 1 | hello what what what | 1 | what |
| 1 | hello | 1 | hello what what what | 1 | what what |
+----+---------+------------+----------------------+------------+-----------+
mysql> select * from message left join comments on id = comments.message_id left join likes on id = likes.message_id;
+----+---------+------------+----------------------+------------+-----------+
| id | message | message_id | comment | message_id | comment |
+----+---------+------------+----------------------+------------+-----------+
| 1 | hello | 1 | hello | 1 | what |
| 1 | hello | 1 | hello what what | 1 | what |
| 1 | hello | 1 | hello what what what | 1 | what |
| 1 | hello | 1 | hello | 1 | what what |
| 1 | hello | 1 | hello what what | 1 | what what |
| 1 | hello | 1 | hello what what what | 1 | what what |
| 2 | two | 2 | bye | 2 | what ok |
+----+---------+------------+----------------------+------------+-----------+
我同意喜欢应该是一个整数,但这些数据是我刚才编写的模拟数据。
答案 0 :(得分:2)
您只需使用LEFT JOIN
代替INNER JOIN
:
SELECT *
FROM message M
LEFT JOIN comments C ON M.id = C.message_id
LEFT JOIN likes L ON M.id = L.message_id
有关不同类型连接的更多信息,请访问:
What's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN?
答案 1 :(得分:0)
也许尝试一下,看看它是否有帮助;
select * from message
left join comments on id = comments.message_id
left join likes on id = likes.message_id
如需进一步阅读,请查看; http://www.w3schools.com/Sql/sql_join_left.asp
答案 2 :(得分:0)
是的,左连接会将所有行保留在该连接表达式的左表或第一个表中,所以这可能是你想要的:
SELECT * 来自消息 LEFT JOIN注释ON id = comments.message_id LEFT JOIN喜欢ON id = likes.message_id