简体   繁体   English

在MySQL中选择WHERE子句的所有行

[英]Select all rows with WHERE clause in MySQL

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. 发件人收件人是引用第二个表的id的外键。

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. 但它只显示sender = recipient的行。 How can I show all rows with unique data in each row? 如何在每行中显示包含唯一数据的所有行?

Use LEFT JOIN instead of INNER JOIN 使用LEFT JOIN而不是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;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM