[英]UPDATE query from SELECT array in one statment
Hi I have 2 SQL statements 嗨,我有2条SQL语句
Statement 1: 陈述1:
SELECT id, name, version FROM mydb WHERE device IS NULL AND Activated=1 ORDER BY id ASC LIMIT 10
Statement 2: 陈述2:
UPDATE mydb SET device='$device' WHERE name IN ('$itemsArray')
I have been getting the $itemsArray
array from the first statement, but now I need to combine these statements, and I am not sure how to go about it and still keep it efficient. 我已经从第一条语句中获取了$itemsArray
数组,但是现在我需要组合这些语句,而且我不确定如何进行处理并仍然保持其效率。
When I have tried it myself I get 0 rows affected: 当我自己尝试时,受影响的行数为0:
UPDATE mydb SET device='$device' WHERE Name IN (SELECT name FROM mydb WHERE device IS NULL AND Activated=1 ORDER BY id ASC LIMIT 10)
Edit: Removed quotes from subquery. 编辑:从子查询中删除引号。 Now I get this error: "This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'" 现在,我收到此错误:“此版本的MySQL尚不支持'LIMIT&IN / ALL / ANY / SOME子查询'”
Using MySQL Version: 5.7.15 使用MySQL版本:5.7.15
In the MySQL documentation for 5.7 it states "MySQL does not support LIMIT in subqueries for certain subquery operators" 在5.7的MySQL文档中,它声明“某些子查询运算符的子查询中MySQL不支持LIMIT”
http://dev.mysql.com/doc/refman/5.7/en/subquery-restrictions.html http://dev.mysql.com/doc/refman/5.7/en/subquery-restrictions.html
I also found this related post here on subqueries error with the LIMIT keyword: MySQL - This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery 我还在LIMIT关键字的子查询错误中找到了此相关文章: MySQL-此版本的MySQL尚不支持'LIMIT&IN / ALL / ANY / SOME子查询
Hope this helps :) 希望这可以帮助 :)
Use JOIN
to relate the subquery to the table. 使用JOIN
将子查询与表相关联。
UPDATE mydb AS t1
JOIN (SELECT name
FROM mydb
WHERE device IS NULL AND Activated=1
ORDER BY id ASC
LIMIT 10) AS t2 ON t1.Name = t2.Name
SET device = '$device'
This is the correct syntax looks more like this: 这是正确的语法,看起来像这样:
UPDATE mydb
SET device = '$device'
WHERE Name IN (SELECT name FROM mydb WHERE device IS NULL AND Activated=1 ORDER BY id ASC LIMIT 10);
A subquery should not be surrounded by quotes, like a string. 子查询不应像字符串一样用引号引起来。
However, I don't think this will work for two reasons: (1) the table is the same and (2) the limit. 但是,我认为这样做不可行有两个原因:(1)表相同并且(2)限制。
So, this might do what you want: 因此,这可能会满足您的要求:
UPDATE mydb
SET device = '$device'
WHERE device IS NULL AND Activated = 1
ORDER BY id ASC
LIMIT 10;
You might need a more complicated query if you really want to update based on name
and not id
. 如果您确实要基于name
而不是id
进行更新,则可能需要更复杂的查询。
I suggest that you are working too hard. 我建议您太努力了。 Get rid of the SELECT
: 摆脱SELECT
:
UPDATE mydb SET device='$device'
WHERE device IS NULL AND Activated=1
ORDER BY id ASC LIMIT 10;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.