我有一個包含三列的SQL表:SQL單獨一列的數
userId
userName
item
,我創造了這個SQL查詢這會對所有項目類型的一個用戶:
select
count(ItemID) as 'count of all items types',
userId,
userName
from
userTable
where
ItemID in (2, 3, 4)
and userId = 1
group by
userId, userName
其結果將是這樣的:
+--------+----------+--------------------------+
| userId | userName | count of all items types |
+--------+----------+--------------------------+
| 1 | kim | 25 |
,我正在尋找一種方式來itemes類型的計數分開,所以結果應該爲L ike this:
+--------+----------+----------------+----------------+-----------------+
| userId | userName | count of item1 | count of item2 | count of item3 |
+--------+----------+----------------+----------------+-----------------+
| 1 | kim | 10 | 10 | 5 |