簡體   English   中英

SELECT *以及與同一表的另一列的ID相關聯的所有行

[英]SELECT * plus all rows that are associated with the IDs of another column of the same table

我有一張表: user_relationship 我需要選擇一個employee_id所有行以及包含這些client_id所有其他行。

id | employee_id | client_id
1  | 365         | 12
2  | 874         | 63
3  | 365         | 48
4  | 471         | 12
5  | 471         | 48
6  | 522         | 90

SELECT * FROM `user_relationship` WHERE `employee_id` = 471

id | employee_id | client_id
4  | 471         | 12
5  | 471         | 48

但是我需要添加到該查詢中,以便隨后也選擇與返回的client_id相關的所有employee_id

id | employee_id | client_id
1  | 365         | 12
3  | 365         | 48
4  | 471         | 12
5  | 471         | 48

我可以幫忙嗎?

您將需要使用子查詢。 這應該工作。

SELECT * FROM `user_relationship` WHERE `client_id` IN (SELECT `client_id` FROM `user_relationship` WHERE `employee_id` = 471)

這是關於主題https://dev.mysql.com/doc/refman/5.1/en/any-in-some-subqueries.html的文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM