tags:

views:

26

answers:

1

Hi

How to search dob in db is in the yyyy-mm-dd strucuture. But i have to compare only with yyyy of the field using mysql.

example:

id dob

1 1999-05-10 2 2000-10-05

I am having only the year 2000 . Now how can i get the record 2000-10-05 using mysql search query.

thanks in advance

+4  A: 
select * from table where year(dob) = '2010';
cherouvim
thanks chrerovium. but could please explain how year(dob) works
Fero
It extracts only the year part from a specified date (in this case it is the "dob" column). Have a look at http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
cherouvim
thanks for your advice chrerovium.
Fero
Note: This works, but will be slow for large sets, as the function prevents use of an index. Having date as a foreign field into a "dates" table would be more efficient, but also more hassle to implement (as date ops would require at least a join more).
Piskvor
@Piskvor: of course it's a full table scan. Joining dates to another table is an overkill though. A better strategy would probably be to denormalize and repeat the year(dob) in another column with an index on it.
cherouvim
@cherouvim: the "overkill" label very much depends. I've had this problem before and denormalizing was problematic (in other words, coders didn't update the other, denormalized fields -> inconsistent data); I'd say denormalizing or not is up for discussion (although I admit that having a date in a foreign table does look kind of silly).
Piskvor