[英]How does select query works?
我對此查詢有麻煩,它執行得很好,但是我無法確定此select語句的工作方式。 對此問題的任何幫助或解釋,將不勝感激..謝謝您這些是我的表格,在這里查詢正在尋找與他們工作的公司住在同一城市的員工
表格:-emp
eid name street city
----------- ---------------- ------------- ------------
1 yeman asd vasai
2 aksh adssd mumbai
3 chintan ghfgh mumbai
4 samual ghfdgh bandra
5 ddlj fghfgh andheri
6 jack fghnfg Bandra
7 bridge gfhfgh vasai
8 rahim ghfgh mumbai
9 chirag fghfghfg bandra
10 mistry hhhty bandra
11 ravi tytey andheri
表格:-公司
cid companyname city
----------- ------------------- ------------
1 Vasai Industries vasai
2 Mumbai Pharmacy mumbai
3 bandra loft bandra
4 andheri tactics andheri
表格:=作品
eid cid salary
----------- ----------- -----------
1 1 200
2 3 4831
3 4 4457
4 2 20001
5 1 32221
6 2 224
7 3 784
8 1 336
9 3 2489
10 2 4789
11 1 22541
詢問
select * from emp
where eid
IN (select eid from works
where cid=(select cid from company
where city=emp.city))
為什么不將此查詢與聯接一起使用,它為什么易於理解,然后是一堆子查詢。
select * from emp
inner join works on works.eid = emp.eid
inner join company on company.city=emp.city
說明:
1. select cid from company where city=emp.city
在這里,您將獲得有關emp和公司相同的城市的城市ID
2。
select eid from works
where cid=(select cid from company
where city=emp.city)
在這里,您可以從emp和company中cid相同的work表中獲取id的集合。
3。
select * from emp
where eid
IN (select eid from works
where cid=(select cid from company
where city=emp.city))
在這里,您將獲得基於emp id的所有記錄,這些城市的emp和city相同
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.