简体   繁体   中英

Less equal than specific date query not working on equal date

I have this query:

SELECT COUNT(a.No_Registrasi) as Jumlah, b.Nama_Negara, a.Tanggal_Reg
FROM tb_registrasi a
JOIN tb_negara_tujuan b
   ON a.ID_Negara = b.ID_Negara
WHERE a.Tanggal_Reg >= '2016-02-01' AND a.Tanggal_Reg <= '2016-03-01'
GROUP BY b.Nama_Negara

The result is:
结果

But when im change to:

SELECT COUNT(a.No_Registrasi) as Jumlah, b.Nama_Negara, a.Tanggal_Reg
FROM tb_registrasi a
JOIN tb_negara_tujuan b
   ON a.ID_Negara = b.ID_Negara
WHERE a.Tanggal_Reg >= '2016-02-01' AND a.Tanggal_Reg <= '2016-02-29' //this one
GROUP BY b.Nama_Negara

It didnt show any result, what im trying is to select data from between 2 different dates, but when when there's data on the date at the end of the month, it didnt show the data. say i have 5 data that registered to date 2016-04-30, when im selecting data from 2016-04-01 to 2016-04-30 it didnt show any result.
I hope you guys understand what i mean, thanks in advance.

Apply DATE Function to columns

WHERE DATE(a.Tanggal_Reg) >= '2016-02-01' AND DATE(a.Tanggal_Reg) <= '2016-02-29'

or use between

WHERE DATE(a.Tanggal_Reg) BETWEEN '2016-02-01' AND '2016-02-29'

So your query will be:

SELECT COUNT(a.No_Registrasi) as Jumlah, b.Nama_Negara, a.Tanggal_Reg
FROM tb_registrasi a
JOIN tb_negara_tujuan b
   ON a.ID_Negara = b.ID_Negara
WHERE DATE(a.Tanggal_Reg) >= '2016-02-01' AND DATE(a.Tanggal_Reg) <= '2016-02-29' //this one
GROUP BY b.Nama_Negara

or with between

SELECT COUNT(a.No_Registrasi) as Jumlah, b.Nama_Negara, a.Tanggal_Reg
    FROM tb_registrasi a
    JOIN tb_negara_tujuan b
       ON a.ID_Negara = b.ID_Negara
    WHERE DATE(a.Tanggal_Reg) BETWEEN '2016-02-01' AND '2016-02-29' 
    GROUP BY b.Nama_Negara

Have you tried the following query?

SELECT COUNT(a.No_Registrasi) as Jumlah, b.Nama_Negara, a.Tanggal_Reg
FROM tb_registrasi a
JOIN tb_negara_tujuan b
ON a.ID_Negara = b.ID_Negara
WHERE a.Tanggal_Reg >= '2016-02-01 00:00:00' AND a.Tanggal_Reg <= '2016-02-29 23:59:59'
GROUP BY b.Nama_Negara

You may use BETWEEN too as

SELECT COUNT(a.No_Registrasi) as Jumlah, b.Nama_Negara, a.Tanggal_Reg
FROM tb_registrasi a
JOIN tb_negara_tujuan b
   ON a.ID_Negara = b.ID_Negara
WHERE a.Tanggal_Reg BETWEEN '2016-02-01 00:00:00' AND '2016-02-29 23:59:59'
GROUP BY b.Nama_Negara

I think that you have hour's problem. Sql is getting datas between 2016-02-01 00:00:00 and 2016-02-29 00:00:00. And to get a month datas you can get with the next day like that:

WHERE a.Tanggal_Reg >= '2016-02-01' AND a.Tanggal_Reg <= '2016-03-01'

Or you can try give hours to query:

    SELECT COUNT(a.No_Registrasi) as Jumlah, b.Nama_Negara, a.Tanggal_Reg
FROM tb_registrasi a
JOIN tb_negara_tujuan b
   ON a.ID_Negara = b.ID_Negara
WHERE a.Tanggal_Reg >= '2016-02-01 00:00:00' AND a.Tanggal_Reg <= '2016-02-29 23:59:59' 
GROUP BY b.Nama_Negara

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM