Nev
Nev

Reputation: 71

Calculate age based on DOB and return the rows between age 20 to 30

I am trying to calculate age based on DOB stored in user table and get result row based on the age.

e.g. I want users whose age between 20 to 30.

I have tried using the sub-query but no luck.

SELECT
id
FROM
personal_info
WHERE
(
SELECT
    ROUND(
        (
            DATEDIFF(
                CURRENT_DATE,
                STR_TO_DATE(dob, '%Y-%m-%d')
            ) / 365
        )
    ) AS age
FROM
    personal_info
) BETWEEN 20 AND 30

Upvotes: 0

Views: 88

Answers (1)

Fahmi
Fahmi

Reputation: 37473

Try below query:

SELECT
id
FROM
personal_info
WHERe 
    ROUND
        (
            DATEDIFF(
                CURRENT_DATE,
                STR_TO_DATE(dob, '%Y-m%-%d')
            ) / 365
        ) BETWEEN 20 AND 30

Upvotes: 1

Related Questions