The following is my MySQL table. I want to select a particular range of values from my table hello
.
name age job gender
A 33 dgfd m
b 44 gdfg f
c 21 jhkh m
e 23 etertr m
How would I select a male whose age falls into the age category of 20-30 years.
SELECT hello.*
WHERE hello.age='20-30' AND hello.gender='m';
You can use a WHERE
clause to filter the data:
select name, age, job, gender
from hello
where age >=20
and age <=30
and gender = 'm'
See SQL Fiddle with Demo
This can also be written using BETWEEN
:
select name, age, job, gender
from hello
where age between 20 and 30
and gender = 'm'
See SQL Fiddle with Demo.
Typically you will want to store a date of birth instead of the age
of a person, then the age can be calculated when needed.
SELECT name
FROM hello
WHERE age BETWEEN 20 AND 30
AND gender = 'm'
Don't store age
. Store a date field and calculate the age. What would happen if the person got older?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With