TABLE 1 包含名字和姓氏,TABLE 2 包含两列引用 TABLE 1 上的姓名

TABLE 1 with name and surname and TABLE 2 with two columns reference to names on TABLE 1

我不是程序员,我从这个表格中阅读了很多关于如何解决我的问题的信息,但我的搜索并不好

我有两张桌子

TABLE 1: 成员

id*| name | surname
-------------------
1  | Joe  | Smith
2  | Mary | Sullivan
3  | Will | Stevenson

TABLE 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

我真的真的真的真的迷路了 任何人都可以帮忙吗?谢谢!

您需要加入 members table 2 次 messages

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`

尝试:

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

您需要写一个 Select 和别名来引用成员 table 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