在mysql中update用的很多,一般大家所知道的就是传统的操作,如下:
UPDATE [LOW_PRIORITY] [IGNORE] table_reference
SET assignment_list
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]
下面给大家介绍一个小例子,实现跨表更新。
需求是:将ai_campaign_transaction表的中id_entity字段,更新到ai_transaction表中。这就涉及到了跨表批量更新。
实例如下:
update ai_transaction as ai
join ai_campaign_transaction as dc
on ai.report_date = dc.report_date
and ai.campaign_id = dc.campaign_id
set ai.id_entity = dc.id_entity
where report_date >= '2018-08-20';
基本结构是:
update 表名1 as 别名1
join 表名2 as 别名2
on ...
set ...
where ...
其中join可以根据需求改为left join 、right join等。