MySQL最大日期查询
我需要找到证书的最长日期才能将存档字段设置为0。问题在于并非所有的认证都有有效期。现有的更新查询如下所示:MySQL最大日期查询,mysql,Mysql,我需要找到证书的最长日期才能将存档字段设置为0。问题在于并非所有的认证都有有效期。现有的更新查询如下所示: update user_certs uc1 join ( select user_id, cert_id, ifnull(max(date_expire),max(cert_date)) cert_date from user_certs group by user_id,cert_id ) uc2 on (uc1.user_id=uc2.user_id a
update user_certs uc1
join ( select user_id, cert_id, ifnull(max(date_expire),max(cert_date)) cert_date
from user_certs
group by user_id,cert_id ) uc2 on (uc1.user_id=uc2.user_id and uc1.cert_id=uc2.cert_id and ifnull(uc1.date_expire,uc1.cert_date)=uc2.cert_date)
set uc1.archive=0
update user_certs uc1
join ( select user_id, cert_id, c.expiration, max(case when date_expire > cert_date + interval c.expiration month then date_expire else cert_date end ) cert_date
from user_certs
join certifications c on c.id = cert_id
group by user_id,cert_id ) uc2 on (uc1.user_id=uc2.user_id and uc1.cert_id=uc2.cert_id and (case when uc1.date_expire > uc1.cert_date + interval uc2.expiration month then uc1.date_expire else uc1.cert_date end) = uc2.cert_date)
set uc1.archive=0;
在某些情况下,查询不考虑同一证书的多个条目。其中一个可能有一个到期日期,但其他可能有一个较新的证书日期,但到期日期为空,因为记录尚未从权威数据库导入
下面的查询将解释该条件,但我无法使其正常工作。即使我将c.expiration值硬编码为36而不是加入,它也会不断返回一个无效使用组函数错误
update user_certs uc1
join ( select user_id, cert_id, max(case when date_expire > cert_date + interval c.expiration month then date_expire else cert_date end ) cert_date
from user_certs
join certifications c on c.id = cert_id
group by user_id,cert_id ) uc2 on (uc1.user_id=uc2.user_id and uc1.cert_id=uc2.cert_id and max(case when uc1.date_expire > uc1.cert_date + interval uc2.expiration month then uc1.date_expire else uc1.cert_date end )=uc2.cert_date)
set uc1.archive = 0;
因此,我需要根据到期日期或证书日期+证书到期(c.到期)中的较大者(以月为单位)查找最新的证书
更新:
在Jim MC的评论帮助下解决。更正的查询如下:
update user_certs uc1
join ( select user_id, cert_id, ifnull(max(date_expire),max(cert_date)) cert_date
from user_certs
group by user_id,cert_id ) uc2 on (uc1.user_id=uc2.user_id and uc1.cert_id=uc2.cert_id and ifnull(uc1.date_expire,uc1.cert_date)=uc2.cert_date)
set uc1.archive=0
update user_certs uc1
join ( select user_id, cert_id, c.expiration, max(case when date_expire > cert_date + interval c.expiration month then date_expire else cert_date end ) cert_date
from user_certs
join certifications c on c.id = cert_id
group by user_id,cert_id ) uc2 on (uc1.user_id=uc2.user_id and uc1.cert_id=uc2.cert_id and (case when uc1.date_expire > uc1.cert_date + interval uc2.expiration month then uc1.date_expire else uc1.cert_date end) = uc2.cert_date)
set uc1.archive=0;
我不是这样问这个问题的。你的语法错误是由于在ON子句中使用MAX(它抱怨的组函数)引起的。麦克斯在那里说不通。谢谢@JimMc!把它弄明白并发布了工作查询。