Kumar Rakesh Kumar Rakesh - 5 months ago 12
MySQL Question

Select query - Fetch all data from two age min and max age

Mind burning query for me.
I want to fetch the all record between two age condition. Any one please help me. My query is write below

SELECT tbl_trip.* FROM tbl_trip WHERE ((tbl_trip.minage >='15' AND tbl_trip.maxage <='15') OR (tbl_trip.maxage <= '28' AND tbl_trip.minage >='28'))

In this query i want all record from database .. where age lie between 15 to 28 . In this all reords get, e.g. minage to maxage : 1 to 16,12 to 30 , 16 to 24, 27 to 28 But not get like 3 to 13 or 29 or 100 . Thanks in advance.


This query checks your given range between minage and maxage, and vice versa.

select * from tbl_trip 
where (((minage>=15 and minage<=22)or(maxage>=15 and maxage<=22))
or((15>=minage and 15<=maxage)or(22<=maxage and 22>=minage)))

This is also working, Please check it properly before you use:

select * from tbl_trip 
where ((minage>=12 and minage<=22)or(maxage>=12 and maxage<=22))
       or((12>=minage and 22<=maxage))

Here is another way:

select * from tbl_trip
Where GREATEST(GREATEST(minage,12)-LEAST(maxage,22),0)=0