2011-08-22 84 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