Rumah > Soal Jawab > teks badan
P粉1477476372023-07-25 10:55:52
Sila tambah syarat HAVING pada pertanyaan anda:
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
Jika anda hanya perlu mengira bilangan bandar unik, anda boleh menggunakan COUNT(DISTINCT(city)) dan mengalih keluarnya daripada kumpulan seperti ini:
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