我有2個表格,基本上我喜歡做的是將結果或計數組合在一起進行顯示。嘗試了不同版本的mysql語句,但沒有得到任何地方。Mysql查詢 - 分組結果
的2個示例表格是:
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
我有2個表格,基本上我喜歡做的是將結果或計數組合在一起進行顯示。嘗試了不同版本的mysql語句,但沒有得到任何地方。Mysql查詢 - 分組結果
的2個示例表格是:
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
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;
加入表,則組的結果:
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。
簡單的東西,你可以,試試這個:
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;