使用内部表连接的Mysql更新查询

时间:2017-06-30 13:05:40

标签: mysql sql

我在mysql中更新查询有问题。 我试试这个时

update planned_expense p1 
set deleted=1 
where case_id=204 
  and deleted =0  
  and type='MONTHLY' 
  and planned_date>='2017-04-01'  
  and id > (select min(id) from planned_expense p2 where 
             p2.case_id = p1.case_id 
               and  p2.planned_date = p1.planned_date 
               and  p2.account = p1.account 
               and p2.type = p1.type and p2.deleted = 0)

我得到了

您无法指定目标表' p1'用于FROM子句中的更新

当我尝试

update planned_expense p1  
set deleted=1 
where case_id=204 
and deleted =0   
and type='MONTHLY'  
and planned_date>='2017-04-01'   
and id > (select min(id)  
    from (select * from planned_expense p2  
    where p2.case_id=p1.case_id and   
    p2.planned_date=p1.planned_date  
    and p2.account=p1.account and p2.type=p1.type and p2.deleted=0) p3)

我得到了

未知栏' p1.case_id'在' where子句'

为了更新这些记录,我应该写什么? Tnank你。<​​/ p>

这是我的表

DROP TABLE IF EXISTS `bes-ers`.`planned_expense`;
CREATE TABLE  `bes-ers`.`planned_expense` (
  `ID` bigint(20) NOT NULL AUTO_INCREMENT,
  `case_id` bigint(20) DEFAULT NULL,
  `deleted` tinyint(1) DEFAULT '0',
  `planned_date` datetime DEFAULT NULL,
  `addition_mark` int(11) DEFAULT NULL,
  `code` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `planned_amount` decimal(22,4) DEFAULT NULL,
  `account` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `type` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `search_field` varchar(4096) COLLATE utf8_unicode_ci DEFAULT NULL,
  `description` varchar(4096) COLLATE utf8_unicode_ci DEFAULT NULL,
  `created_by_id` bigint(20) DEFAULT NULL,
  `locked` tinyint(4) DEFAULT '0',
  PRIMARY KEY (`ID`),
  KEY `FK_planned_expenses_1` (`case_id`),
  KEY `FK_planned_expenses_created_by_id` (`created_by_id`),
  KEY `planned_expense_planned_date` (`planned_date`),
  KEY `planned_expense_type` (`type`),
  KEY `planned_expense_deleted` (`deleted`),
  CONSTRAINT `FK_planned_expenses_1` FOREIGN KEY (`case_id`) REFERENCES `bankruptcy_case` (`ID`) ON DELETE SET NULL ON UPDATE SET NULL,
  CONSTRAINT `FK_planned_expenses_created_by_id` FOREIGN KEY (`created_by_id`) REFERENCES `user` (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=13172954 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

3 个答案:

答案 0 :(得分:0)

如果涉及select,Mysql不允许更新表,但您可以使用dinamica选择表来避免此问题,例如:

    update planned_expense 
      set deleted=1 
      where case_id=204 
      and deleted =0  
      and type='MONTHLY' 
      and planned_date>='2017-04-01'  
      and id > ( select t.min_id from (select min(id)  min_id 
                from planned_expense p2 
                INNER JOIN planned_expense p1 on p2.case_id=p1.case_id 
                        and  p2.planned_date=p1.planned_date 
                    and p2.account=p1.account 
                    and p2.type=p1.type and p2.deleted=0) t ) 

尝试添加复合索引

create index idx_test on planned_expense (case_id, planned_date, account)

答案 1 :(得分:0)

如@scaisEdge所述,您必须使用联接并加快查询速度,
尝试根据where子句创建复合键 类似的东西:
KEY case_id_deleted_type_planned_date_id(case_id,deleted,type,planned_date,id)
使用Explain检查您的查询是否使用了正确的索引。

答案 2 :(得分:0)

谢谢大家,我通过创建临时表而不是使用它来做我想做的事。

感谢