In the database I have table users which have column 'birthday'
Here is the sql query that I use to fetch the results
SELECT *
FROM (`users`)
WHERE `birthday` between '12/11/1982' and '31/01/1983'
The problems is that I get records only for year 1982 ?
Notice that records for 1983 year exists.
What could be the problem here ?
This is just an alternative answer from Mark Byers. If the data type of your column birthday
is varchar
and has format of dd/MM/yyyy
, please do read this; otherwise, read from Mark Byer's .
The first thing you need to do is to convert your column into DATE
or DATETIME
datatype by using STR_TO_DATE
,
SELECT *
FROM `users`
WHERE STR_TO_DATE(`birthday`,'%d/%m/%Y')
between '1982-01-12' and '1983-12-31'
SOURCES
You should put the year first in your date literals:
SELECT *
FROM `users`
WHERE `birthday` between '1982-11-12' AND '1983-01-31'
From the documentation :
Date and time values can be represented in several formats, such as quoted strings or as numbers, depending on the exact type of the value and other factors. For example, in contexts where MySQL expects a date, it interprets any of '2015-07-21', '20150721', and 20150721 as a date.
If you are using a varchar
type then it still makes sense to put the date first (both in the data and in the query) because then your dates will sort correctly when using the default sorting order.
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.