简体   繁体   English

MySQL查询以显示发送方和接收方的名称

[英]MySQL query to display name from sender and receiver

I have a table called users which looks like this: 我有一个称为users的表,看起来像这样:

 +----+----------------+
 | id | name           | 
 +----+----------------+
 |  1 | Blake          | 
 |  2 | Jenn           | 
 +----+----------------+

And i have a table called msg which looks like this: 我有一个名为msg的表,看起来像这样:

 +----+----------------+----------------+
 | id | sender         | receiver       | 
 +----+----------------+----------------+
 |  1 | 1              | 2              |
 |  2 | 2              | 1              |
 +----+----------------+----------------+

So now i have a problem because i can't figure out how to join msg.sender(id) to users.name(name). 所以现在我有一个问题,因为我不知道如何将msg.sender(id)加入users.name(name)。

So basicly what i want to end up with, looks kind of like this: 所以基本上我想要结束的事情看起来像这样:

 +----+----------------+----------------+----------------+----------------+
 | id | sender         | sender_name    | receiver       | receiver_name  |
 +----+----------------+----------------+----------------+----------------+
 |  1 | 1              | Blake          | 2              | Jenn           |
 |  2 | 2              | Jenn           | 1              | Blake          |
 +----+----------------+----------------+----------------+----------------+

I hope these illustrations kind of help with what I'm trying to explain. 希望这些插图对我要解释的内容有所帮助。

Just join the users table twice with msg table - Once on sender and then on receiver. 只需将users表与msgjoin两次-在发送者上然后在接收者上。

select m.*,
    s.name as sender_name,
    r.name as receiver_name
from msg m
join users s on m.sender = s.id
join users r on m.receiver = r.id;

You should join the user table twice using alias 您应该使用别名两次连接用户表

  select  msg.sender, u1.name  as sender_name, msg.receiver, u2.name as receiver_name
  from msg
  inner join users u1 on u1.id =  msg.sender
  inner join users u2 on u2.id =  msg.receiver

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 私人聊天系统MYSQL查询以显示发送者/接收者的最后一条消息 - Private chat system MYSQL query to display last message of sender/receiver MySQL聊天系统显示发件人/收件人的最后一条消息和其他人的姓名 - MySQL chat system to display last message of sender/receiver AND name of other person 由MySQL中的发送方接收方或接收方发送方分组 - Group by sender receiver or receiver sender in MySQL mysql查询以显示带有发件人和收件人的消息 - mysql query to display messages with sender and reciever MySQL查询显示名称从a到p范围 - Mysql query to display name from a to p range 如果发送者 id 和接收者 id 值在任何列中进行数学运算,Mysql 将显示一个列 - Mysql display one colum if sender id and receiver id values mathes in any of the colums MYSQL - 查询获取不同的 sender_id、receiver id 和 richiesta_id - MYSQL - Query for getting distinct sender_id and receiver id and richiesta_id 如何从表2中获取具有发送者和接收者名称的所有数据,这些名称与user(table1)表中的id链接 - how to get all data from table2 with sender and receiver name which linked with id from user(table1) table 如何在mysql中检索带有发送者和接收者用户名的聊天消息 - How to retrieve a chat msg with sender and receiver username in mysql 如何获取 mysql 上发件人 ID 和收件人 ID 之间的所有消息? - How to get all messages between sender id and receiver id on mysql?
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM