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