mysql update不能直接使用select的结果
在
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
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
例:
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
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
例:
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