我有2张桌子,基本上我喜欢做的是将结果或计数分组以便显示。尝试了不同版本的mysql语句,但没有到达任何地方。
两个示例表是:
tbl_One
index O_priority
1 low
2 medium
3 high
tbl_Two
t_priority
2
1
3
3
2
3
1
1
1
expected results:
low = 4
medium = 2
high = 3
答案 0 :(得分:1)
SELECT T1.O_priority,T2.c FROM tbl_One as T1 LEFT JOIN (SELECT count(*) as c,t_priority FROM tbl_Two GROUP BY t_priority) as T2 ON T1.index = T2.t_priority;
答案 1 :(得分:0)
加入表格,然后对结果进行分组:
SELECT tbl_One.O_priority, COUNT(*)
FROM tbl_One JOIN tbl_Two ON tbl_Two.t_priority = tbl_One.index
GROUP BY tbl_One.index
在sqlfiddle上查看。
答案 2 :(得分:0)
尽可能简单,试试这个:
SELECT count(o.index) as `index`, o.O_priority
FROM tbl_One o join tbl_two t on t.t_priority = o.index
group by t.t_priority;