Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 具有两个不同ID的SQL查询_Mysql_Sql_Count_Sql Update_Subquery - Fatal编程技术网

Mysql 具有两个不同ID的SQL查询

Mysql 具有两个不同ID的SQL查询,mysql,sql,count,sql-update,subquery,Mysql,Sql,Count,Sql Update,Subquery,我需要防止photo_order字段超过与文章关联的照片数量。我有这个,但不起作用。表名已经使用了两次,可能是错误的 UPDATE articles_photos SET photo_order = IF(photo_order < (SELECT COUNT(id) FROM articles_photos WHERE article_id = 12), photo_order + 1, 1) WHERE id = 26 如何修复上述查询?我的数据库是MySQ

我需要防止photo_order字段超过与文章关联的照片数量。我有这个,但不起作用。表名已经使用了两次,可能是错误的

UPDATE articles_photos SET photo_order = 
    IF(photo_order < (SELECT COUNT(id) FROM articles_photos
        WHERE article_id = 12), photo_order + 1, 1) WHERE id = 26

如何修复上述查询?我的数据库是MySQL。

我想这就是您得到的错误:

无法在中指定要更新的目标表“articles\u photos” 条款

下面是一个将交叉联接与子查询结合使用的解决方案:

update articles_photos ap
cross join (select count(id) cnt from articles_photos where article_id = 12) temp
set ap.photo_order = if(ap.photo_order<temp.cnt,ap.photo_order+1,1)
where ap.id = 26;