如何使用一个 SQL-query select 发件人(数据)和收件人(数据)?
How to select the sender(data) and the recipient(data) using one SQL-query?
所以,我正在使用 PHP 和 MySQL。我有一个 table 'notifications' 和一个 table 'users'。通知给出了发送者和接收者。我做了一些研究并尝试了一些事情,但我无法找到解决方案。以下 SQL 查询为我提供了通知中的数据集以及收件人数据,但我如何在一个查询中 select 来自用户 table 的发件人数据?
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;
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
所以,我正在使用 PHP 和 MySQL。我有一个 table 'notifications' 和一个 table 'users'。通知给出了发送者和接收者。我做了一些研究并尝试了一些事情,但我无法找到解决方案。以下 SQL 查询为我提供了通知中的数据集以及收件人数据,但我如何在一个查询中 select 来自用户 table 的发件人数据?
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;
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