简体   繁体   中英

CASE checking expiration in select statement in MySQL query

I am using MySQL case and I don't know what is my problem in my query. The error comes from update statement. And I don't know if my idea works. Thanks in advance.

This is my query

 SELECT CASE WHEN (date(au.expiration) < date(now()))
 THEN (UPDATE controller SET access = '0' WHERE idno = au.assignedidno)
 ELSE (UPDATE controller SET access = '1'  WHERE idno = au.assignedidno)
 END FROM assignuser au 

Please help.

I think you need an UPDATE statement with a join of the 2 tables and setting the value to the column with a CASE expression:

UPDATE controller c 
INNER JOIN assignuser au
ON c.idno = au.assignedidno
SET c.access = CASE 
  WHEN (date(au.expiration) < date(now())) THEN '0'
  ELSE '1'
END

This can be simplified like this:

UPDATE controller c 
INNER JOIN assignuser au
ON c.idno = au.assignedidno
SET c.access = (date(au.expiration) >= date(now())) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM