To calculate age from a birth date in MySQL, you can use the DATEDIFF
function to calculate the difference between the birth date and the current date, and then divide the result by the number of days in a year (365.25) to get the age in years. Here's an example:
In this example, birth_date
is the name of the column that contains the birth date, and mytable
is the name of the table that contains the birth date column. The NOW()
function returns the current date and time, and DATEDIFF
calculates the difference between the birth date and the current date in days. The result is then divided by 365.25 (the average number of days in a year) and rounded to the nearest whole number using the ROUND
function.
You can modify this query to suit your specific needs, such as by using a different table or column name, or by specifying a specific birth date instead of using NOW()
. Keep in mind that this method may not be completely accurate due to leap years, but it should give you a good approximation of the person's age.
No comments:
Post a Comment