mysql 2查询成1

时间:2010-10-23 08:09:00

标签: mysql

$sql = $empire -> query("select * from issue 
where disabled='0' order by issue desc");
    while($r=$empire->fetch($sql)){
        //get total
        $total = $empire -> gettotal("select count(*) as 
        total from sendbook where issueid='".$r["issue"]."'");

        $list .= 'id: '.$r['id'].' issue :'.$r['issue'].' total:'.$total.'';
    }
echo $list;

id:1 issue: 23 total:10
id:6 issue: 24 total:0
id:7 issue: 24 total:15

如何使用1个查询来完成这项工作?

想你!

2 个答案:

答案 0 :(得分:1)

您可以尝试:

SELECT id, issue,(SELECT count(issueid) AS count FROM sendbook WHERE (issueid = issue)) AS total 
FROM issue WHERE disabled='0' 
GROUP BY issue 
ORDER BY issue DESC;

答案 1 :(得分:0)

SELECT issue.*, COUNT(*) AS total
FROM issue AS i
LEFT JOIN sendbook AS s ON i.issue = s.issueid
WHERE i.disabled='0' 
GROUP BY i.issue
ORDER BY i.issue DESC

这将是这些方面的事情。没有测试代码,所以你可能需要调整......