我有一个包含用户名的user
表和一个包含message
和from_id
的{{1}}表。如何只使用一个查询从用户名和到用户名获取?
我使用了以下查询,但出了点问题:
to_id
select id,name
from user
where 'id' not in (select 'from_id' from message) and
'id' not in (select 'to_ID' from message);
表格
user
表格
答案 0 :(得分:0)
获取用户的用户名和ID
select id, name from user u, message m where u.id=m.from_ID and u.id=m.to_ID
答案 1 :(得分:0)
试试这个:
SELECT m.id 'message_id',
`u1`.`name` `from_name`,
`u2`.`name` `to_name`
FROM `message` `m`
JOIN `user` `u1` ON `m`.`from_id` = `u1`.`id`
JOIN `user` `u2` ON `m`.`to_id` = `u2`.`id`
答案 2 :(得分:0)
SELECT from.name as FromUser, to.name AS ToUser
FROM user AS from, user AS to, (SELECT * FROM message where id=somevalue) AS m
WHERE from.emp_id = m.from_id AND to.emp_id=m.to_id;