tags:

views:

41

answers:

2
$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

how to use 1 query to finish this work?

Think you!

+1  A: 

You can try:

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;
Nervo Verdezoto
You right : SELECT id, issue, (SELECT count(*) AS count FROM sendbook WHERE (sendbook.issueid = issue.id)) AS total FROM issue WHERE disabled='0' ORDER BY issue desc
BRAVO
A: 
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

It will be something in these lines. Didn't test the code so you may have to adjust...

Frankie