如何在查询中计算年龄?年龄搜索

时间:2017-07-04 22:59:30

标签: mysql

你好我有一张桌子除以年月和日 enter image description here

如何计算输入中使用它的年龄? 调用输入的代码是..

if(isset($keyage)){


$keyag = "AND bday_month LIKE '".$keyage."'";




}  

但研究仅以月为基础,我如何看待年龄 - 谢谢

1 个答案:

答案 0 :(得分:0)

尝试根据您的样本数据计算您的年龄:

select case when m_now>m_table then age 
            when m_now=m_table THEN
                 case when d_now=d_table then age ELSE
                       case when d_now<=d_table then age-1 else age end

       end
ELSE age -1 END as age from

(select day(date(now()))d_now,day(date(CONCAT_WS('-',bday_year,bday_month,bday_day))) d_table,
month(date(now()))m_now, month(date(CONCAT_WS('-',bday_year,bday_month,bday_day)))m_table,
year(date(now()))-year(date(CONCAT_WS('-',bday_year,bday_month,bday_day))) age from
(Select 4 bday_month, 1 as bday_day , 1981 bday_year) as a) as a