$ i table:id,item_name
$ t table:id,member_name,item_name
$ u table:id,member_name,membership_status
我想
这是我到目前为止所回答的问题的第一部分:
$find_actives = mysql_query ("select $i.item_name,
count($t.item_name) from $i left
join $t on $i.item_name = $t.item_name group by $t.item_name")
但是当我将其更改为下面的查询时,它不再列出$ i中的所有项目。
$find_actives = mysql_query ("select $i.item_name,
count($t.item_name) from $i left
join $t on $i.item_name = $t.item_name
where $t.member_name IN (select distinct member_name from $u
where membership_status = 'active' group by member_name)
group by $t.item_name")
我希望结果如下:
item_name1 - count = 5
item_name2 - count = 0 //这是缺失的行
item_name3 - count = 2
我怎样才能做到这一点?
答案 0 :(得分:0)
如果我正确地阅读你的问题,我认为这就是你要找的:
SELECT
$i.item_name,
COUNT(temp.item_name) AS item_count
FROM $i
LEFT JOIN (
SELECT $t.item_name AS item_name
FROM $t
INNER JOIN $u
ON $t.member_name = $u.member_name
WHERE $u.membership_status = 'active'
) AS temp
ON $i.item_name = temp.item_name
GROUP BY $i.item_name