1. 程式人生 > >mysql update不能直接使用select的結果 mysql update不能直接使用select的結果

mysql update不能直接使用select的結果 mysql update不能直接使用select的結果

mysql update不能直接使用select的結果

2016年04月25日 10:53:37 閱讀數:569 sql  server中,我們可是使用以下update語句對錶進行更新:
update a set a.xx= (select yy from b) ;
但是在mysql中,不能直接使用set select的結果,必須使用inner join:
update a inner join (select yy from b) c set a.xx = c.yy
mysql <wbr>update不能直接使用select的結果
例: update mb_tariff a inner join    mb_tariff_temp b  set a.payment =  b.payment  where a.mybus_id = b.mybus_id  and a.tariff_id = b.tariff_id 

例二  mysql


UPDATE account AA
INNER JOIN (

SELECT
c.AccountId,
c.money,
c.zjf - c.money AS zj,
c.kyjf - c.money AS ky
FROM
(
SELECT
b.AccountId,
b.money money,
(
SELECT
y.Money
FROM
account y
WHERE
y.id = b.AccountId
) zjf,
(
SELECT
y.UseableMoney
FROM
account y
WHERE
y.id = b.AccountId
) kyjf

FROM
(
SELECT
d.AccountId,
d.money
FROM
account a
LEFT JOIN wateraccount d ON a.id = d.AccountId
WHERE
d.FromApp = 'LEVEL_service'
AND (a.id = 83 OR a.id = 86)
GROUP BY
a.id
) b
) c

) cc
SET AA.money = cc.zj
WHERE
cc.AccountId = AA.Id







sql  server中,我們可是使用以下update語句對錶進行更新:
update a set a.xx= (select yy from b) ;
但是在mysql中,不能直接使用set select的結果,必須使用inner join:
update a inner join (select yy from b) c set a.xx = c.yy mysql <wbr>update不能直接使用select的結果
例: update mb_tariff a inner join    mb_tariff_temp b  set a.payment =  b.payment  where a.mybus_id = b.mybus_id  and a.tariff_id = b.tariff_id