我有一個這種結構的表
---------- ----------
| user_id | tema_id |
---------- ----------
| 1 | 1 |
| 2 | 1 |
| 3 | 2 |
| 4 | 3 |
| 5 | 2 |
| 6 | 3 |
| 7 | 1 |
---------- ----------
我只想在一個查詢中得到不同tema_idby的總數tema_id。我有這個查詢,但它回傳不同tema_id的但計數列到一而不是總和tema_id。
SELECT tema_id, COUNT(DISTINCT(tema_id)) as total
FROM push_subscriptions
GROUP BY tema_id
回傳這個:
---------- ----------
| tema_id | total |
---------- ----------
| 1 | 1 | -> must be 3
| 2 | 1 | -> must be 2
| 3 | 1 | -> must be 2
---------- ----------
謝謝
uj5u.com熱心網友回復:
一個簡單count(*)的應該做的伎倆:
select tema_id, count(*) as total
from push_subscriptions
group by tema_id;
小提琴
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/438535.html
下一篇:如何修復“應使用逗號或右括號”?
