Mysql修改年齡

2021-10-20 17:07:48 字數 577 閱讀 6506

資料庫(mysql)中經常需要修改年齡,一般根據身份證號修改,或是根據出生日期修改;做個簡單的記錄:

1.需要根據身份證號修改年齡,這個語句只是判斷了18位身份證

update  `表名稱` t

set t.`age` = (substring(now(),1,4)-substring(t.`身份證號`,7,4))-(substring(t.`身份證號`,11,4)-date_format(now(),'%m%d')>0)

where length(t.`id_number`) = 18

and substr(t.`id_number`, 11, 2)+0 <= 12

and substr(t.`id_number`, 11, 2) <> '00'

and substr(t.`id_number`, 13, 2)+0 <= 31

and substr(t.`id_number`, 13, 2) <> '00';

2.根據出生日期修改:

update  `表名稱` t

set t.`age` = timestampdiff(year, `出生日期`, curdate());

mysql 修改年齡 MYSQL 資料修改

mysql的資料修改 1 新增單列 a alter table 列表名 add 要加的引數名 如年齡 內容 如21 位於所有列的最下面 b alter table 列表名 add 要加的引數名 如年齡 內容 如21 tirst 新新增的這列位於最前面 c alter table 列表名 add 要加...

mysql計算年齡

方法一,作者也說出了缺陷,就是當日期為未來日期時結果為0,而不是負數 這裡使用了5個函式和兩個運算子。方法二select date format now y date format birthday,y date format now 00 m d date format birthday,00 m...

mysql出生日期計算年齡並查詢年齡

剛開始用mysql,需要用出生日期計算年齡,而且需要查詢符合這個年齡的使用者資訊。網路上介紹了不少顯示年齡的方法,但是要使用where語句查詢時,就失效了。在網路上找到了一位大神的回答,親測有用。題目 查詢年齡為33的使用者資訊 select customers.cusname,customers....