2左连接与联合

时间:2014-10-12 18:14:38

标签: mysql sql

tbl_chat:

message_id  message_text    users_id  another_user   time_sent
1           'hi'                9        1           2014-10-13 00:10:32   
2           'hello'             1        9           2014-10-13 00:12:32
3           'good morning'      9        1           2014-10-13 00:12:34
4           'good night'        9        1           2014-10-13 00:14:02
5           'LOL'               1        9           2014-10-13 00:14:05

tbl_usersinfo:

users_id    users_fname     users_lname
1            ben               ten
9            son               goku

我希望获得这些人的所有对话,并显示他们的全名,消息以及他们发送该消息的时间,但我的查询并未正确返回其名称,这是我的查询:

SELECT CONCAT_WS(' ',i.users_fname, i.users_lname) AS full_name, c.message_text,c.time_sent,c.message_id
FROM tbl_chat AS c
LEFT JOIN tbl_usersinfo AS i ON i.users_id = c.another_user
WHERE c.users_id = 1
UNION
SELECT CONCAT_WS(' ',i.users_fname, i.users_lname) AS full_name, c.message_text,c.time_sent,c.message_id
FROM tbl_chat AS c
LEFT JOIN tbl_usersinfo AS i ON i.users_id = c.users_id
WHERE c.users_id = 9
ORDER BY time_sent ASC

此查询的结果将是:

full_name  message_text        time_sent             message_id
 son goku    'hi'               2014-10-13 00:10:32     1
 son goku    'hello'            2014-10-13 00:12:32     2 
 son goku    'good morning'     2014-10-13 00:12:34     3
 son goku    'good night'       2014-10-13 00:14:02     4
 son goku    'lol'              2014-10-13 00:14:05     5

但我想要的输出是:

full_name  message_text        time_sent             message_id
son goku    'hi'               2014-10-13 00:10:32    1
ben ten     'hello'            2014-10-13 00:12:32    2 
son goku    'good morning'     2014-10-13 00:12:34    3
son goku    'good night'       2014-10-13 00:14:02    4
ben ten     'lol'              2014-10-13 00:14:05    5

3 个答案:

答案 0 :(得分:3)

SELECT CONCAT_WS(' ',i.users_fname, i.users_lname) AS full_name, c.message_text,c.time_sent,c.message_id
FROM tbl_chat AS c
LEFT JOIN tbl_usersinfo AS i ON i.users_id = c.users_id
WHERE (c.users_id = 1 AND c.another_user = 9)
   OR (c.users_id = 9 AND c.another_user = 1)
ORDER BY time_sent ASC

答案 1 :(得分:1)

存在轻微的逻辑错误

SELECT CONCAT_WS(' ',i.users_fname, i.users_lname) AS full_name, c.message_text,c.time_sent,c.message_id
FROM tbl_chat AS c
LEFT JOIN tbl_usersinfo AS i ON i.users_id = c.another_user // this is another user so turning 1 to 9 
WHERE c.users_id = 1
UNION
SELECT CONCAT_WS(' ',i.users_fname, i.users_lname) AS full_name, c.message_text,c.time_sent,c.message_id
FROM tbl_chat AS c
LEFT JOIN tbl_usersinfo AS i ON i.users_id = c.users_id 
WHERE c.users_id = 9
ORDER BY time_sent ASC

要更正此问题,您可以将i.users_id = c.another_user更改为i.users_id = c.users_id,但最好的方法是删除联合并保持查询简单,如@Rimas指出

SELECT CONCAT_WS(' ',i.users_fname, i.users_lname) AS full_name, c.message_text,c.time_sent,c.message_id
FROM tbl_chat AS c
LEFT JOIN tbl_usersinfo AS i ON i.users_id = c.users_id 
WHERE c.users_id in (1,9) AND c.another_user in (1,9)

答案 2 :(得分:0)

好吧,我没有使用任何联合或左联接,假设tbl_chat的userId将始终存在于chatinfo表中。我使用以下内容获得正确的输出,

select concat(users_fname,' ', users_lname), message_text, time_sent , message_id
from tbl_chat t
join tbl_usersinfo u
on u.users_id = t.users_id
order by message_id

根据问题中列出的方案,这可行,这将很快,因为它没有使用任何联合。