Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.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更新查询和子查询有什么问题?_Mysql_Sql - Fatal编程技术网

Mysql更新查询和子查询有什么问题?

Mysql更新查询和子查询有什么问题?,mysql,sql,Mysql,Sql,我有一个这样的问题 UPDATE `user_plan_details` SET `plan_expiry_date` = Date_add((SELECT plan_expiry_date FROM `user_plan_details` WHERE user_id = 56

我有一个这样的问题

UPDATE `user_plan_details` 
SET    `plan_expiry_date` = Date_add((SELECT plan_expiry_date 
                                      FROM   `user_plan_details` 
                                      WHERE  user_id = 56 
                                             AND 
                                     user_plan_details.is_current_plan = 1) 
                                   , INTERVAL 30 day) 
WHERE  `user_id` = '56' 
       AND user_plan_details.is_current_plan = 1 
执行此查询时,我收到如下错误消息

MySQL数据库错误:无法在FROM子句中为更新指定目标表“user\u plan\u details”

子查询

SELECT DATE_ADD((SELECT plan_expiry_date FROM `user_plan_details` WHERE user_id = 56 AND user_plan_details.is_current_plan = 1 ), INTERVAL 30 DAY)
给出了结果

1/11/2013 12:00:00 AM
我的问题是什么?请帮助我。

请参阅“子查询中未正确使用的表”中的最后一项

它表示可以在update语句中使用子查询,但不能在update和subselect中使用相同的表

但是你可以试试这个

UPDATE `user_plan_details` 
SET    `plan_expiry_date` = Date_add(plan_expiry_date, INTERVAL 30 day) 
WHERE  `user_id` = '56' 
       AND user_plan_details.is_current_plan = 1 

用于测试。

原因是您正在更新一个表,该表的记录是从该表的子查询中获取的。MYSQL不允许这样做,您需要通过动态创建临时表来愚弄数据库

你可以简单地::

UPDATE `user_plan_details` 
SET    `plan_expiry_date` = Date_add(plan_expiry_date, INTERVAL 30 day) 
WHERE  `user_id` = '56' 
       AND user_plan_details.is_current_plan = 1
或者,如果你想以你的风格来做: as::

您可以这样做:

update user_plan_details as t1, (select DATE_ADD((SELECT plan_expiry_date FROM 
   `user_plan_details` WHERE  user_id = 56 AND user_plan_details.is_current_plan = 1 ),
    INTERVAL 30 DAY) as t2 ) as t3  set t1.plan_expiry_date=t3.t2 where t1.`user_id` = 
    '56' AND t1.is_current_plan = 1 ;

如果你的代码格式不正确,我不会帮你的
update user_plan_details as t1, (select DATE_ADD((SELECT plan_expiry_date FROM 
   `user_plan_details` WHERE  user_id = 56 AND user_plan_details.is_current_plan = 1 ),
    INTERVAL 30 DAY) as t2 ) as t3  set t1.plan_expiry_date=t3.t2 where t1.`user_id` = 
    '56' AND t1.is_current_plan = 1 ;