我想加入2张桌子......
表“用户”:
列:
uid =>用户索引
name =>用户名
表“消息”:
列:
fromid =>发件人用户索引
toid =>接收者用户指数
message =>短信
我想要一个像这样的输出: | from_username | to_username |消息|
我可以得到| from_username |消息| 使用此查询:
SELECT messages.message, users.name FROM messages,users WHERE messages.fromid = users.uid;
答案 0 :(得分:1)
您需要完成两次加入:
Select f.name, t.name, message from messages
Left join users as f on messages.fromID = f.uid
Left join users as t On messages.toid = t.uid
对于完整的连接语法(以及一些解释),MySQL文档页面不错http://dev.mysql.com/doc/refman/5.0/en/join.html
答案 1 :(得分:0)
尝试
SELECT sender.name, receiver.name, message
FROM messages
INNER JOIN users sender
ON messages.fromid = sender.uid
INNER JOIN users receiver
ON messages.toid = receiver.uid