SQL计数出现的条目

时间:2018-03-08 17:35:30

标签: mysql sql

我在计算数据库中的某些条目时遇到了一些问题。

我有以下查询:

SELECT ref_training_date, training_date FROM fynslund.users_training
INNER JOIN training
ON training.id = users_training.ref_training_date
WHERE attendance = 1

这给了我以下结果:

┌──────┬───────────────┐
│ ID   │      DATE     │
├──────┼───────────────┤
│ '55' │  '2018-01-09' │
│ '55' │  '2018-01-09' │
│ '54' │  '2018-02-03' │
│ '54' │  '2018-02-03' │
│ '54' │  '2018-02-03' │
│ '54' │  '2018-02-03' │
└──────┴───────────────┘

如何计算ID为'55'的日期显示的次数?

1 个答案:

答案 0 :(得分:0)

您需要GOUP BY ref_training_date然后COUNT(training_date)

SELECT ref_training_date, COUNT(training_date) AS date_count
FROM fynslund.users_training
INNER JOIN training ON training.id = users_training.ref_training_date
WHERE attendance = 1
GROUP BY ref_training_date