2011-08-22 128 views
0

我有兩個疑問幫助內部聯接查詢

$query1="SELECT staffid, COUNT(staffid) FROM enrll GROUP BY staffid ORDER BY COUNT(staffid) DESC"; 
$query2="SELECT staffid FROM staff as s WHERE auth = '1' AND NOT EXISTS (SELECT staffid from shdl as h where s.staffid=h.staffid and h.shdldt='".$unixdt."')"; 

兩個查詢在陣列中的返回值,我想找出從QUERY1 STAFFID那些具有QUERY2內部聯接和值將在數組中。

注意什麼將組合查詢兩個查詢返回最終查詢。

回答

1
select e.staffid, count(e.staffid) 
from enrll e 
join staff s on e.staffid=s.staffid 
where s.auth = '1' and NOT EXISTS ( 
    SELECT staffid from shdl as h 
    where s.staffid=h.staffid and h.shdldt='$unixdt') 
group by e.staffid 
ORDER BY COUNT(staffid) DESC 
+0

@john:是不是他們需要寫COUNT(e.staffid)? JOIN需要定義爲INNER JOIN? – Aditii

+0

@Aditii,你對'COUNT(e.staffid)'說得對,這是一個錯誤,修正。 'join'是'inner join'的別名,所以沒問題。 – J0HN

+0

我不會在where子句中使用子查詢... – binaryLV