-1

所以,我正在使用 PHP 和 MySQL。我有一个表“通知”和一个表“用户”。通知给出了发送者和接收者。我做了一些研究并尝试了一些东西,但我无法找到解决方案。以下 SQL 查询为我提供了通知中的数据集以及收件人数据,但我如何在一次查询中从用户表中选择发件人数据?

SELECT notifications.id,notifications.recipient_id,notifications.sender_id,notifications.unread,notifications.type,notifications.parameters,notifications.created_at,users.id AS user_id,users.username 
FROM notifications, users 
WHERE users.id = notifications.recipient_id;
4

1 回答 1

1
SELECT 
    notifications.id,
    notifications.recipient_id,
    notifications.sender_id,
    notifications.unread,
    notifications.type,
    notifications.parameters,
    notifications.created_at,
    users1.id AS user_id_recipient,
    users1.username  AS username_recipient,
    users2.id AS user_id_sender,
    users2.username  AS username_sender
FROM notifications
INNER JOIN users AS users1 ON users1.id = notifications.recipient_id
INNER JOIN users AS users2 ON users2.id = notifications.sender_id
于 2017-02-24T21:40:01.890 回答