5

我有一个 Select 查询,它返回所有时间少于 8 天的会话的会话 ID。效果很好!

SELECT sessionID FROM session WHERE sessionStatus = 'open' GROUP BY sessionID HAVING MAX(refTime) <= 8;

但是,我正在尝试更新表,以便会话 ID 小于 8 天的每条记录都将其 sessionStatus 更改为“关闭”。从stackoverflow我知道我无法更新我也从中选择的表,并且Having和Group By是使这更复杂的agerate函数。

我试过这个,但没有骰子!

UPDATE session 
SET sessionStatus='closed'
WHERE sessionID = (select * from (SELECT MAX(sessionID) FROM session where sessionStatus = 'open') as t);

我真的很感激任何帮助!

4

2 回答 2

4

这是我在这种情况下使用的解决方法:

CREATE TEMPORARY TABLE tempsessions AS SELECT MAX(sessionID) AS sessionID FROM session where sessionStatus = 'open';
UPDATE session SET sessionStatus = 'closed' WHERE sessionID IN (SELECT sessionID FROM tempsessions);
DROP TEMPORARY TABLE tempsessions;
于 2012-05-23T14:32:02.003 回答
2

也试试这个——

UPDATE
  session s1
JOIN
  (SELECT MAX(sessionID) sessionID FROM session WHERE sessionStatus = 'open') s2
    ON s1.sessionID = s2.sessionID
SET
  s1.sessionStatus = 'closed';
于 2012-05-23T14:56:36.723 回答