0
id | userid | exerciseid | date | time | weight | distance | reps
1 | 24 | 1 | 2013-09-28 00:00:00 | 2321 | 231 | 121 | NULL
2 | 24 | 24 | 2013-09-28 00:00:00 | 2321 | 231 | 121 | NULL
3 | 24 | 1 | 2013-09-28 00:00:00 | 2321 | 231 | 121 | NULL
4 | 24 | 1 | 2000-00-00 00:00:00 | NULL | 100 | NULL | 2
5 | 24 | 1 | 2013-09-28 00:00:00 | 2321 | 231 | 121 | NULL
行1,3和5是相同的。我想做一個將它們分組在一起的計數,同時也添加一個包含計數值的列。SQL:計數和分組行
SELECT id, userid, exerciseid, date, time, weight, distance, reps
FROM `exercises`
WHERE `userid` = 1 AND `date` < now()
所以,我想這回也類似的東西:
id | userid | exerciseid | date | time | weight | distance | reps | count
1 | 24 | 1 | 2013-09-28 00:00:00 | 2321 | 231 | 121 | NULL | 3
4 | 24 | 1 | 2000-00-00 00:00:00 | NULL | 100 | NULL | 2 | NULL
是否有可能擁有數列如果1是NULL? – ditto
您可以用IF(COUNT(*)= 1,NULL,COUNT(*))替換count(*)AS數 –