Home  >  Article  >  Database  >  mysqlupdateselect_MySQL

mysqlupdateselect_MySQL

WBOY
WBOYOriginal
2016-05-27 14:12:091362browse

bitsCN.com

在sql server中,我们可是使用以下update语句对表进行更新:

update a set a.xx= (select yy from b) where a.id = b.id ;

但是在mysql中,不能直接使用set select的结果,必须使用inner join:

update a inner join (select yy from b) c on a.id =b.id set a.xx = c.yy

示例代码:

多表连接更新

UPDATE spgl_goods a INNER JOIN(SELECT g.`ID` AS idFROM rkgl_goods_repertory gr,rkgl_repertory r,spgl_goods gWHERE 1=1AND gr.`REPERTORY_ID`=r.`ID`AND gr.`GOODS_ID`=g.`ID`AND g.`IS_DELETE`=FALSEAND r.`USER_ID`=36GROUP BY gr.`GOODS_ID`) b ON a.`ID`=b.id SET a.`IS_DELETE`=TRUE;
bitsCN.com
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn