我有以下2个表格。
首先是:idea_box
第二是:idea_box_voting
现在我希望得到类似这样的结果,基于来自thumb field的0 and 1 count
。
我从未创建像这样的SQL查询,所以我希望有人帮助我。
感谢。
答案 0 :(得分:1)
试试这个:
SELECT a.idea_id, a.property_id, a.the_idea, a.user_id, a.added_date, a.status,
SUM(b.thumbs = 1) AS up, SUM(b.thumbs = 0) AS down
FROM idea_box a
LEFT JOIN idea_box_voting b ON a.idea_id = b.idea_id
GROUP BY a.idea_id;
答案 1 :(得分:0)
试试这个:
SELECT ib.*,
SUM(ibv.thumbs=1) as UP,
SUM(ibv.thumbs=0) as DOWN
FROM idea_box ib LEFT JOIN
idea_box_voting ibv on ib.idea_id=ibv.idea_id
GROUP BY ib.idea_id
SQL Fiddle中的示例。
答案 2 :(得分:0)
SELECT t1。*,sum(t2.thumbs = 1)as up,sum(t2.thumbs = 0)as down table1 as t1 left join table2 as t2 using(idea_id)group by t1.idea_id;