sql查询

t1qtbnec  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(225)

项目:及时记录,不定时记录/
我想更新最新条目(假设有多个条目具有null in out time)的out time(条目为null)。
sql表:rollnumber,date,intime,outtime。
在运行代码时,它给出了语法错误。

ps = con.prepareStatement("WITH q AS (SELECT * FROM ENTRY WHERE\
    (ROLLNUMBER =?AND DATE = ? AND OUTTIME IS NULL) OR (ROLLNUMBER = ? AND DATE = ? AND
    OUTTIME IS NULL) ORDER BY INTIME DESC LIMIT 1)
     UPDATE q SET OUTTIME = ?");
euoag5mw

euoag5mw1#

在mysql的旧版本中,正确的语法是:

UPDATE entry
    SET OUTTIME = ?
    WHERE (ROLLNUMBER = ? AND DATE = ? AND OUTTIME IS NULL) OR
          (ROLLNUMBER = ? AND DATE = ? AND OUTTIME IS NULL)
    ORDER BY INTIME DESC
    LIMIT 1;

老实说,这看起来比使用cte的版本简单。请注意,参数的顺序发生了一些变化。

soat7uwm

soat7uwm2#

MySQL8中提供了
对于以前的版本,可以将updatejoin与子查询一起使用,例如:

UPDATE q
  inner join (
  SELECT * FROM ENTRY 
    WHERE (ROLLNUMBER =? AND DATE = ? AND OUTTIME IS NULL) 
    OR (ROLLNUMBER = ? AND DATE = ? AND OUTTIME IS NULL) 
    ORDER BY INTIME DESC LIMIT 1)

    ) t on t.t_key = q.q_key
    SET OUTTIME = ?

相关问题