P粉1477476372023-07-25 10:55:52
请在您的查询中添加HAVING条件:
SELECT user_id, city, COUNT(*) as result FROM visits WHERE start_ts >= 1675209600 -- 1675209600 = 01.02.2023 00:00 AND end_ts <= 1676419200 -- 1676419200 = 15.2.2023 00:00 GROUP BY user_id, city HAVING result = 1
如果您需要仅计算唯一城市的数量,您可以使用COUNT(DISTINCT(city))并将其从分组中移除,如下所示:
SELECT user_id, city, COUNT(DISTINCT(city)) as result FROM visits WHERE start_ts >= 1675209600 -- 1675209600 = 01.02.2023 00:00 AND end_ts <= 1676419200 -- 1676419200 = 15.2.2023 00:00 GROUP BY user_id HAVING result = 1