I have two tables: The First has these columns:
id
sender
recipient
description
date
and the second has these columns:
id
name
and sender and recipient are foreign key that reference to id of the second table.
I try to select all rows in the first table.
SELECT a.id, a.description, b.name as sender, b.name as recipient, a.date FROM table_A a JOIN table_B b WHERE a.sender=b.id AND a.recipient=b.id ORDER BY date DESC
But it shows only rows where sender = recipient. How can I show all rows with unique data in each row?
Use LEFT JOIN
instead of INNER JOIN
Try this:
SELECT a.id, a.description, b.name AS sender, c.name AS recipient, a.date
FROM table_A a
LEFT JOIN table_B b ON a.sender = b.id
LEFT JOIN table_B c ON a.recipient = c.id
ORDER BY a.date DESC;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.