我不是程序员,我从这个表格中读到了很多关于如何解决我的问题,但我的搜索并不好
我有两张桌子
表1:成员
id*| name | surname
-------------------
1 | Joe | Smith
2 | Mary | Sullivan
3 | Will | Stevenson
表2:消息
---------------------------------
id_message*| from | to | message
---------------------------------
1 | 2 | 1 | test
2 | 1 | 2 | re:test
3 | 3 | 1 | hi
*自动增加字段
我希望进行查询,列出所有消息,如下所示:
Mary Sullivan | Joe Smith | test
Joe Smith | Mary Sullivan | re:test
Will Stevenson | Joe Smith | hi
我真的真的迷路了 有人可以帮忙吗?谢谢!
答案 0 :(得分:1)
您需要使用members
messages
表2次
select
concat(mem1.name,' ',mem1.surname) as `from_name`,
concat(mem2.name,' ',mem2.surname) as `to_name`,
m.message
from messages m
join members mem1 on mem1.id = m.`from`
join members mem2 on mem2.id = m.`to`
答案 1 :(得分:0)
尝试:
select from1.name+" "+from1.surname, to2.name+" "+to2.surname, message from table2
join table1 from1 on table2.`from` = table1.id
join table1 to2 on table2.`to` = table1.id
答案 2 :(得分:0)
你需要写一个Select with alias来引用成员表2次:
SELECT CONCAT(M1.surname, ' ', M1.name) as FROM, CONCAT( M2.surname, ' ', M2.name) as TO FROM
members M1 INNER JOIN
messages M on M.from = M1.id
INNER JOIN messages M2 on M.to = M2.id