从不同的表中选择多行

时间:2017-07-18 10:09:24

标签: mysql sql join

我在mysql数据库中有3个表。像群组,群组成员,消息。

| group_id |  group_name  | group_image
-------------------------------------------
| group_1    | First Group  | group1.jpg
| group_2    | 2nd Group    | group2.jpg
| group_3    | 3rd Group    | group3.jpg
| group_4    | 4th Group    | group4.jpg
| group_5    | 5th Group    | group5.jpg
| group_6    | 6th Group    | group6.jpg

讯息

| sender_id |  group_id  | message | time 
-----------------------------------------
| 001    | group_1    | first message | 2017-07-13 15:05:07
| 002    | group_2    | fifth message | 2017-07-13 15:04:07
| 002    | group_2    | third Message | 2017-07-13 15:03:07

GroupsMembers

| user_id |  group_id 
-----------------------------------------
| 001    | group_1 
| 001    | group_2 
| 002    | group_2 
| 002    | group_1 
| 002    | group_4 
| 002    | group_6 

我正在尝试使用此查询

SELECT mmbr.group_id, grp.group_name, grp.group_image, msg.time, msg.message FROM `GroupsMembers` AS mmbr JOIN `Messages` AS msg ON msg.group_id =mmbr.group_id AND msg.time = (SELECT MAX(time) FROM `Messages` WHERE group_id = mmbr.group_id) JOIN `Groups` AS grp ON grp.group_id = mmbr.group_id WHERE mmbr.user_id = '002'

它给了我以下结果: -

| group_id |  group_name    | group_image | time                | message |
------------------------------------------------------------------------
| group_1    | First Group  | group1.jpg  | 2017-07-13 15:05:07 | first message
| group_2    | 2nd Group    | group2.jpg  | 2017-07-13 15:04:07 | fifth message

但是 需要 所有 群组 userid '002' 消息 ,不带某事 喜欢 : -
需要 上次 已发送 消息 如果 任何

| group_id   |  group_name  | group_image | time                | message |
------------------------------------------------------------------------
| group_1    | First Group  | group1.jpg  | 2017-07-13 15:05:07 | first message
| group_2    | 2nd Group    | group2.jpg  | 2017-07-13 15:04:07 | fifth message
| group_4    | 4th Group    | group4.jpg  |  null               |  null
| group_6    | 6th Group    | group6.jpg  |  null               |  null  

3 个答案:

答案 0 :(得分:0)

尝试LEFT OUTER JOIN:

SELECT mmbr.group_id,
       grp.group_name,
       grp.group_image,
       msg.time,
       msg.message
FROM   `GroupsMembers` AS mmbr
JOIN   `Groups` AS grp
  ON   grp.group_id = mmbr.group_id
LEFT OUTER JOIN `Messages` AS msg
             ON msg.group_id = mmbr.group_id
            AND msg.time = (SELECT MAX(time)
                            FROM `Messages`
                            WHERE group_id = mmbr.group_id)
WHERE  mmbr.user_id = '002';

答案 1 :(得分:-2)

考虑使用LEFT Join

SELECT mmbr.group_id, grp.group_name, grp.group_image, msg.time, msg.message FROM `GroupsMembers` AS mmbr 
JOIN `Groups` AS grp ON grp.group_id = mmbr.group_id 
LEFT JOIN `Messages` AS msg ON msg.group_id =mmbr.group_id AND msg.time = (SELECT MAX(time) FROM `Messages` WHERE group_id = mmbr.group_id) 
WHERE mmbr.user_id = '002' 

阅读本教程https://www.w3schools.com/sql/sql_join_left.asp

答案 2 :(得分:-2)

申请LEFT JOIN。

试试这个

SELECT mmbr.group_id,grp.group_name,grp.group_image,msg.time,msg.message FROM GroupsMembers AS mmbr LEFT JOIN消息AS msg ON msg.group_id =mmbr.group_id AND msg.time = (SELECT MAX(time) FROM消息WHERE group_id = mmbr.group_id) LEFT JOIN群组`AS grp ON grp.group_id = mmbr.group_id WHERE mmbr.user_id ='002'