提问人:Laci K 提问时间:10/31/2023 最后编辑:PM 77-1Laci K 更新时间:10/31/2023 访问量:24
UPDATE 列,其中第一个不为空的出现项来自另一个与 id 匹配的表
UPDATE column with the first not empty occurrence from another table that matching id
问:
我想使用另一个表中的第一个不为空的名称值更新一个非常大的表中的名称,但另一个表存储了多种语言名称,并且无法找出一个有效的查询。
表 a 如下所示:
id | name
1 | test name
2 | test name 2
表 b 如下所示:
id | table_a_id | name | lang_id
1 | 1 | new name | 1
2 | 1 | | 2
3 | 1 | new name in other lang | 3
4 | 2 | | 1
5 | 2 | other new name | 2
6 | 2 | | 3
我想得到的结果:
表A
id | name
1 | new name
2 | other new name
我该怎么做?
答:
0赞
Turo
10/31/2023
#1
Straightforward 是一个具有子选择和限制 1 的解决方案:
update tablea a
set a.name = (SELECT name FROM
tableb b1
where name is not null
and b1.tablea_id = a.id
order by language_id
limit 1);
正如比尔·考林(Bill Kawrin)所说,这不会是最快的......
评论
SELECT VERSION();
lang_id