Mysql更新内部连接表中的行
本文关键字:连接 更新 内部 Mysql | 更新日期: 2023-09-27 18:03:15
我试图通过c#在mysql服务器上批量更新大量行。
我知道可以使用语法
批量替换INSERT INTO table.items(`id`, `asset_id`, `bot_steam64`)
VALUES(...),
(...),
(...),
ON DUPLICATE KEY UPDATE asset_id=...
我想对表中
的部分执行此操作- 与另一个表共享a字段值
- 匹配另一个表中的特定字段值(这听起来像一个连接)
我不知道如何表达我所描述的复合逻辑,或者是否有另一种方法来表达它
items:
asset_id bigint(32) UN
trade_id bigint(32) UN
value int(11)
name tinytext
bot_steam64 bigint(20)
winner tinyint(4)
id bigint(32) UN PK
bets:
trade_id bigint(32) UN
user_steam64 bigint(20) UN PK
match_id bigint(32) UN PK
total_value bigint(32) UN
chosen_team tinyint(3) UN
具体为items.trade_id=bets.trade_id
和match_id=1234
。
create table t1
(
i int not null,
some_other_thing int not null
);
create table t2
( i int not null,
updateMe int not null,
some_other_thing int not null
);
insert into t1(i,some_other_thing) values (1,1),(2,7);
insert into t2(i,updateMe,some_other_thing) values (1,0,0),(2,0,4);
update t2
join t1
on t2.i=t1.i and t2.some_other_thing=4
set t2.updateMe=9;
-- 1 row changed
select * from t2
如果需要对t1进行修改,可以根据需要调整。Some_other_thing in join.
我不做insert on duplicate update
的原因是因为你说你想做一个更新在开始。你说部分的表
也许你想要这样的东西:
INSERT INTO table.items(`id`, `asset_id`, `bot_steam64`)
SELECT t.*
FROM (SELECT ... UNION ALL
SELECT ... UNIONALL
. . .
) t
WHERE t.col IN (SELECT a.anothercol FROM anothertable a)
ON DUPLICATE KEY UPDATE asset_id = ...;