Following is my sql query for cron in which i am trying to save records from one table to another table i just want one modification which i could not make that in my i_age field the age should get save in the db but if zero is comming from previous table then the age calculation formula doesnot apply and it saves only zero to i_age field kindly let me know how can i do that ,
THANKS,
INSERT IGNORE into z_census ( i_age)
SELECT IF((i_age = 0), i_age, FLOOR(DATEDIFF(DATE(NOW()),DATE_ADD('1970-01-01',INTERVAL users.i_dob SECOND))/365.25) )
FROM users
This is equivalent to the expression in your query that calculates “age” (in integer) years:
This expression is really of the form:
where
dobis a DATE value that represents the user’s date of birth. We derivedobusing this expression:(This is based on some assumptions, as noted below.)
Alternative:
This calculation returns nearly the same result, but is actually more precise:
NOTE: Again, in this expression,
dobrepresents an actual MySQL DATE value, NOT an integer number of seconds. To use this, you would replace all five occurrences ofdobwith this expression:More details:
It looks like the datatype of the
users.i_dobcolumn represents a date as an integer number of seconds from midnight Jan 1, 1970. (Given that this seems to be “working” for DOB after 1970, but not earlier. So this is most likely a (signed)INTorBIGINT. (It’s possible this is a character column, and the value is being implicitly converted to numeric. A TIMESTAMP column would not allow for storage of date values earlier than Jan 1, 1970.)(Having information about the actual datatypes of the columns involved would help some.)
Given that leading “i_” on the column name, we’re going to go forward under the assumption the
users.i_dobcolumn is defined asINT, and represents the number of seconds from midnight Jan 1, 1970 UTC.That gives an effective range of “dob” values that can be represented of approx. ‘1901-12-14’ to ‘2038-01-19’
It looks like this query:
is attempting to calculate a users “age” in years.
That
FROM_UNIXTIMEfunction works with TIMESTAMP values, so it’s not going to “work” for values before Jan 1, 1970.To get the actual DATETIME value represented by i_dob, we can use the
DATE_ADDfunction.To get a number of days between that DATETIME value and the current date, we use the
DATEDIFFfunction:(The DATEDIFF function ignores the time components, and only operates on the date portion, which is most likely what you want. The calculation in your query includes the time portion, which adds a bit of sloppiness.)
From the number of days, it looks like you are calculating years by dividing by number of days in a year, there’s a tiny bit of sloppiness there, when NOW() is within a day of their birthday… but I’m not going to bother with demonstrating that here.)
So, I think this should get it close enough for you,
A more precise calculation of a persons “age” really needs to compare the year, month and day. If we are only interested in reporting non-negative ages, something like this will work:
NOTE: the
dobin that expression represents a MySQLDATEvalue representing the “dob”, NOT an integer number of seconds.NOTE: this expression will returns only non-negative values; it will never return a negative age.
I admit this looks a bit complicated, but all that’s really doing is checking that the age is not at least 1 year, and otherwise subtracting the years, and then using a conditional test (which that returns a boolean we interpret as an integer 1 or 0) to adjust the difference back by 1 year when the current month and day are prior to the month and day of the birthday.)
In your case, to use this, you’d need to substitute all five occurrences of
dobin that expression with the expression that gets dobDATEvalue from the integer, i.e.That would give a more precise result, but would be way more uglier than the other expression, which returns a nearly equivalent result.