SQL select near by values -
i have table "t_person
" 2 fields search by: height int, birthdate datetime
.
when query table want rows values near searched. example when search people height of 175 cm, this:
select * t_person height = 175
i rows height value near 175. eg 174, 173,176...
same thing date column. when search '2003-06-25', dates close it.
is possible?
you need sort of measure on constitutes close. suspect want exact matches appear first. so, order rows "nearness" use:
select p.* t_person p order abs(height - 175);
filtering results useful, can add where
clause:
select p.* t_person p height between 175 - 2 , 175 + 2 order abs(height - 175);
the same idea can work dates. however, don't specify database in question , date functions highly database specific.
Comments
Post a Comment