簡體   English   中英

在MySQL中選擇WHERE子句的所有行

[英]Select all rows with WHERE clause in MySQL

我有兩個表:第一列有這些列:

id 
sender 
recipient 
description
date

第二列有這些列:

id
name

發件人收件人是引用第二個表的id的外鍵。

我嘗試選擇第一個表中的所有行。

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

但它只顯示sender = recipient的行。 如何在每行中顯示包含唯一數據的所有行?

使用LEFT JOIN而不是INNER JOIN

嘗試這個:

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