我有这张桌子:
City. Date
Paris: 2024-04-28
Lyon : 2024-04-28
Lens : 2024-04-28
Paris: 2024-04-29
Lyon: 2024-04-29
London: 2024-04-30
Swindon:2024-04-30
Paris: 2024-05-01
Metz : 2024-05-01
...
我想知道每次约会有多少个城市出现?所以类似:
2024-04-28: 3
2024-04-29: 2
2024-04-30: 2
2024-05-01: 2
我试过这个:
select date, count(*) from table group by date;
select count(city), date from table group by date;
select count(distinct city), date from table group by date;
但它们都不起作用。当我运行这些时,我得到了一些奇怪的数字,例如:
2024-04-28: 50
。
每天计算不同的城市就是您要搜索的
CREATE tABLE event (city varchar(10), `Date` date);
INSERT INTO event VALUES
('Paris', '2024-04-28'),
('Lyon', '2024-04-28'),
('Lens', '2024-04-28'),
('Paris', '2024-04-29'),
('Lyon', '2024-04-29'),
('London', '2024-04-30'),
('Swindon', '2024-04-30'),
('Paris', '2024-05-01'),
('Metz', '2024-05-01');
Records: 9 Duplicates: 0 Warnings: 0
SELECT COUNT(DISTINCT city), `Date` FROM event GROUP BY `Date`;
COUNT(不同城市) | 日期 |
---|---|
3 | 2024-04-28 |
2 | 2024-04-29 |
2 | 2024-04-30 |
2 | 2024-05-01 |