我有以下SQL查询,可在MySQL服务器上进行批量更新:
UPDATE res_booking AS rbg
SET rbg.host_id = (
SELECT vw.entity_id FROM res_booking AS rb
INNER JOIN pass_single as ps ON (ps.book_id = rb.booking_id)
INNER JOIN voucher_who as vw ON (vw.pass_id = ps.pass_id)
WHERE rb.booking_id = rbg.booking_id
)
WHERE rbg.host_id IS NULL;
执行它,给我以下错误:
Error Code: 1093. You can't specify target table 'rbg' for update in FROM clause
该查询如何被重写,因此可以正常工作?
答案 0 :(得分:1)
我会将其写为更新联接:
UPDATE res_booking AS rbg
INNER JOIN res_booking AS rb
ON rb.booking_id = rbg.booking_id
INNER JOIN pass_single AS ps
ON ps.book_id = rb.booking_id
INNER JOIN voucher_who AS vw
ON vw.pass_id = ps.pass_id
SET
rbg.host_id = vw.entity_id
WHERE
rbg.host_id IS NULL;
从技术上讲,res_booking
上的自联接应该是左联接,因为无论子查询是否返回值,您的当前更新都将始终分配给每个记录。但是,我怀疑内部联接逻辑正是您真正想要的。
答案 1 :(得分:0)
您可以使用相关子查询。只需将a = [0, 15, 50 , 75]
dfs = [df.iloc[a[i]:a[i+1]] for i in range(len(a)-1)]
留在子查询之外:
res_booking
如果您在子查询中重新使用正在更新的表,则MySQL会产生错误。但是,它不是必需的。只需修复相关子句以使用UPDATE res_booking rbg
SET rbg.host_id = (SELECT vw.entity_id
FROM pass_single ps JOIN
voucher_who vw
ON vw.pass_id = ps.pass_id
WHERE ps.book_id = rbg.booking_id
)
WHERE rbg.host_id IS NULL;
表即可。
请注意,如果子查询返回多个行,这可能会返回错误。这是一件好事。如果发生这种情况,您将需要弄清楚如何处理这种情况。