【问题标题】:Help with inner join query内部连接查询帮助
【发布时间】:2011-08-22 06:28:39
【问题描述】:

我有两个问题

$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 中找出那些与 query2 有内部联接的人员 ID,并且值将在数组中。

注意两个查询的组合查询以返回最终查询。

【问题讨论】:

    标签: php mysql


    【解决方案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
    

    【讨论】:

    • @john: 他们不需要写 COUNT(e.staffid) 吗? JOIN 需要定义为 INNER JOIN 吗?
    • @Aditii,COUNT(e.staffid) 是对的,这是一个错字,已修复。而joininner join的别名,所以没关系。
    • @binaryLV 我也是。但是我没有数据库结构,所以我不会推测用连接或类似的东西替换这个子查询。所以这取决于 OP。