苦苦于执行嵌套更新查询

时间:2014-08-11 11:28:43

标签: mysql

我知道有1-2个人之前曾问过这个问题但在我的情况下却没有帮助过我。我在做的是 -       我写了以下查询来更新表 -

UPDATE tbl_query_master SET view_status=1 WHERE query_id IN (SELECT query_id FROM tbl_query_master WHERE view_status=0 ORDER BY query_date ASC LIMIT 2);

但它给了我一个跟随错误

#1235 - 此版本的MySQL尚不支持'LIMIT& IN / ALL / ANY / SOME子查询' ”。我尝试过以下的事情 -

1]我已分别执行内部查询 (即SELECT query_id FROM tbl_query_master WHERE view_status = 0 ORDER BY query_date ASC LIMIT 2) 然后它给了我一个预期的结果

2]此外,我已将内部查询的硬编码值替换为“ (UPDATE tbl_query_master SET view_status = 1 WHERE query_id IN(52,53)) ”这种情况它给了我一个预期的结果。谢谢..!

1 个答案:

答案 0 :(得分:1)

为什么不使用加入呢?

UPDATE tbl_query_master t1 
   INNER JOIN (
      SELECT query_id FROM tbl_query_master 
      WHERE view_status=0 
      ORDER BY query_date ASC LIMIT 2) 
   as t2 on t1.query_id = t2.query_id 
SET t1.view_status=1 ;

首先尝试SELECT,看看它是否正确

SELECT * from tbl_query_master t1 
   INNER JOIN (
      SELECT query_id FROM tbl_query_master 
      WHERE view_status=0 
      ORDER BY query_date ASC LIMIT 2) 
   as t2 on t1.query_id = t2.query_id;