Question

I am trying to query a huge database (aprroximately 20 millions records) to get some data. This is the query I am working on right now.

SELECT a.user_id, b.last_name, b.first_name, c.birth_date FROM users a
INNER JOIN users_signup b ON a.user_id a = b.user_id
INNER JOIN users_personal c ON a.user_id a = c.user_id
INNER JOIN
(
SELECT distinct d.a.user_id FROM users_signup d
WHERE d.join_date >= '2013-01-01' and d.join_date < '2014-01-01'
) 
AS t ON a.user_id = t.user_id

I have some problems trying to retrieve additional data from the database. I would like to add 2 additional field to the results table:

  1. I am able to get the birth date but I would like to get the age of the members in the results table. The data is stored as 'yyyy-mm-dd' in the users_personal table.
  2. I would like to get the total days since a member joined till the day the left (if any) from a table called user_signup using data from join_date & left_date (format: yyyy-mm-dd).
Was it helpful?

Solution 2

Try this:

SELECT a.user_id, b.last_name, b.first_name, c.birth_date, 
       FLOOR(DATEDIFF(CURRENT_DATE(), c.birth_date) / 365) age, 
       DATEDIFF(b.left_date, b.join_date) workDays
FROM users a
INNER JOIN users_signup b ON a.user_id a = b.user_id
INNER JOIN users_personal c ON a.user_id a = c.user_id
WHERE b.join_date >= '2013-01-01' AND b.join_date < '2014-01-01'
GROUP BY a.user_id

OTHER TIPS

Or you can do just this ...

SELECT
    TIMESTAMPDIFF(YEAR, birthday, CURDATE()) AS age_in_years,
    TIMESTAMPDIFF(MONTH, birthday, CURDATE()) AS age_in_month,
    TIMESTAMPDIFF(DAY, birthday, CURDATE()) AS age_in_days,
    TIMESTAMPDIFF(MINUTE, birthday, NOW()) AS age_in_minutes,
    TIMESTAMPDIFF(SECOND, birthday, NOW()) AS age_in_seconds
FROM
    table_name

You can use datediff function to find number of days between two days like

select datediff(date1,date2) from table


select datediff(curdate(),date2) from table

Getting the current age in years:

SELECT DATE_FORMAT(FROM_DAYS(DATEDIFF(DATE(NOW()), birthday)), '%Y') * 1 AS age FROM table_name;

How this works:

  1. datediff(date1, date2) gives the difference between two dates in days. Note that the date format of 'birthday' here is date: YYYY-MM-DD.
  2. from_days converts days into a date format
  3. date_format function extracts with '%Y' only the four digit year. Don't use '%y', because you only get a two digit year and some people are older then 99 years.
  4. multiply the string with 1. This is a 'hack'. MySQL will convert a string like 'YYYY' into an integer.

Getting the current age in month (unlikley, but someone may need this)

SELECT (DATE_FORMAT(FROM_DAYS(DATEDIFF(DATE(NOW()), birthday)), '%Y') * 1 * 12)
+ (DATE_FORMAT(FROM_DAYS(DATEDIFF(DATE(NOW()), birthday)), '%m') * 1) AS age_in_months
FROM table_name;

How this works:

  1. Mostly the same as age in years above.
  2. The years get muliplied by 12. A (earth) year has 12 months.
  3. In the next step the months are extracted the same way as the years, but instead the flag '%Y' must be changed to '%m'.
  4. At the end the two values are added together.

Getting the current age in days is as simple as this:

SELECT DATEDIFF(DATE(NOW()), birthday) AS age_in_days FROM table_name;

Alternative code:

SELECT
    DATE_FORMAT(age_date, '%Y') * 1 AS age_in_years,
    (DATE_FORMAT(age_date, '%Y') * 1 * 12) + (DATE_FORMAT(age_date, '%m') * 1) AS age_in_months,
    age_in_days
FROM
    (SELECT 
        FROM_DAYS(DATEDIFF(DATE(NOW()), birthday)) AS age_date,
        DATEDIFF(DATE(NOW()), birthday) AS age_in_days
    FROM table_name) AS age_date;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top