我應該得到1作爲結果,因爲對象3有一個超過零的屬性(a3)。由於groupref加入(這是必要的)我得到3. 我試圖分組,但沒有得到它的工作。MySQL JOIN和GROUP BY
減少聲明
SELECT COUNT(IF(a3 > 0, TRUE, NULL)) AS a3
FROM objects AS o
LEFT JOIN attribs AS a ON o.id=a.oid
LEFT JOIN group_ref AS gx ON gx.oid = o.id
減少表
attribs
------------
oid | attrib | value
------------------------
1 | a1 | 3
2 | a2 | 0
3 | a3 | 1
objects
------------
id | title
------------
1 | obj1
2 | obj2
3 | obj3
groups
------------
id | title
------------
1 | g1
2 | g2
3 | g3
group_ref
------------
gid | oid
------------
1 | 1
1 | 2
2 | 1
3 | 3
3 | 2
3 | 1
的基本結構有留下來,因爲真正的查詢要大得多。 – Mike