Run an instruction in mysql of birth date calculation and update the age column

Asked

Viewed 35 times

-3

I have a column called data_nasc and another age column, but wanted every time the system loaded this instruction into Mysql and updated the age field of all records.

So far it works, but the result comes out in the temporary column ColunaCalculoIdade, I want the result update the age column field

SELECT        TIMESTAMPDIFF(YEAR, data_nasc, CURRENT_DATE) AS ColunaCalculoIdade
FROM            clientes
  • 1

    Why don’t you create a computed column that will calculate this for you? I commented on this in this question: https://answall.com/questions/514534/para-que-serve-a-structura-data-structures-virtual-no-dbeaver-ou-no-mysql/516720#516720

  • remember that only record at include time will be old at that instant, the next day may already be outdated, a computed column will always calculate at the instant of the query :)

  • Usually no age is recorded in the database. Only the date of birth is enough to deduce age.

  • I’ve been looking at the computed column, still very complicated for a beginner, what I want is that when I open the Registration Form the user make a query by age and appear the entries with the searched age or when run the report the age is already updated, as much as I could make the above code.

  • I tried to use it like this: UPDATE clients SET age= SELECT TIMESTAMPDIFF(YEAR, data_nasc, CURRENT_DATE) AS Colunacalculoity FROM clients Just wanted the result of birth date calculation to update my age column.

1 answer

0

I resolved from the following fomra:

I put an event to run 24 HOURS A DAY

CREATE DEFINER=root@localhost EVENT CalculoIdade ON SCHEDULE EVERY 24 HOUR STARTS '2021-07-17 00:00:01' ON COMPLETION PRESERVE UPDATE ENABLE CUSTOMERS SET AGE = TIMESTAMPDIFF(YEAR, data_nasc, CURRENT_DATE)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.