如何更新 MySQL(Aurora DB) 中的多行?
How do I update multiple rows in MySQL(AuroraDB)?
Table Mapper
|-------------------------------------------|
| id | info_fk | reg_fk |
| 1 | 100 | |
| 2 | 101 | |
| 3 | 101 | |
| 4 | 102 | |
| 5 | 102 | |
| 6 | 103 | |
|-------------------------------------------|
Table Region
|---------------------------|
| id | region_id |
| 1 | A101 |
| 2 | A102 |
| 3 | A301 |
| 4 | B101 |
|---------------------------|
Table Info
|--------------------------------------|
| id | address |
| 100 | A101 |
| 101 | A102;A101 |
| 102 | A301;B101 |
| 103 | B101 |
|--------------------------------------|
我想映射信息 table 的 id 并填充 table Mapper。以上示例,我想要的输出是使 [=62=] Mapper 到
Table Mapper
|-------------------------------------------|
| id | info_fk | reg_fk |
| 1 | 100 | 1 |
| 2 | 101 | 1 |
| 3 | 101 | 2 |
| 4 | 102 | 3 |
| 5 | 102 | 4 |
| 6 | 103 | 4 |
|-------------------------------------------|
这个。 reg_fk表示tableRegion的ID,我selectreg_fk基于table信息。 table Mapper 根据 table Info 地址的数量已经存在,所以我所要做的就是阅读Info 的地址并将正确的 ID 放入 Mapper.
这个SQL是我试过的:
with region_info (region_id, info_id) as (
select region.id as region_id, info.id as info_id
from Region region
inner join Info info on
info.address like concat(concat('%', region.region_id), '%')
)
update Mapper mapper inner join Info info
on info_fk = info.id
set mapper.reg_fk = (
case
when info_fk = info.id then
(select region_id
from region_info
where region_info.info_id = info.id
)
end
)
where region_info.info_id = info.id;
它发出错误,我知道我在设置 mapper.reg_fk 时犯了一个错误,有多行我无法将一个 'mapper.reg_fk row' 设置为多行。但是在这个 SQL 句子中,如果我在 info_fk = info.id 上加入信息和映射器,那么我也应该 return 多行。就像在这个例子中一样,info_fk = 100 不会出错,但是 info_fk = 101 然后更新 101 returns 2 行。在 case 语句中,我也得到 2 行(region_id 1 和 2)所以它就像
with join restriction, what to update in table Mapper
|-------------------------------------------|
| id | info_fk | reg_fk |
| 2 | 101 | |
| 3 | 101 | |
|-------------------------------------------|
what appears in case sentence
|--------------------|
| reg_fk |
| 1 |
| 2 |
|--------------------|
我想将 reg_fk 正确更新为 table Mapper。我该怎么做?
在这里您可以简单地构建我的示例数据并测试我的代码(目前无法正常工作)。
http://sqlfiddle.com/#!9/8c0c7d/4
谢谢。
with region_info (region_id, info_id) as (
select region.id as region_id, info.id as info_id
from Region region
inner join Info info on
info.address like concat(concat('%', region.region_id), '%')
)
update Mapper mapper inner join Info info
on info_fk = info.id inner join region_info on info.id = region_info.info_id
set mapper.reg_fk = (
(select region_id
from region_info
where region_info.info_id = info.id
LIMIT 1
)
)
last “其中 region_info.info_id = info.id;”超出子查询。 region_info 在子查询中。所以它找不到 region_info。虽然你用了with子句,但是如果在from子句里,你是找不到的。
您可以使用“LIMIT 1”或“any”(在子查询之前)。
Table Mapper
|-------------------------------------------|
| id | info_fk | reg_fk |
| 1 | 100 | |
| 2 | 101 | |
| 3 | 101 | |
| 4 | 102 | |
| 5 | 102 | |
| 6 | 103 | |
|-------------------------------------------|
Table Region
|---------------------------|
| id | region_id |
| 1 | A101 |
| 2 | A102 |
| 3 | A301 |
| 4 | B101 |
|---------------------------|
Table Info
|--------------------------------------|
| id | address |
| 100 | A101 |
| 101 | A102;A101 |
| 102 | A301;B101 |
| 103 | B101 |
|--------------------------------------|
我想映射信息 table 的 id 并填充 table Mapper。以上示例,我想要的输出是使 [=62=] Mapper 到
Table Mapper
|-------------------------------------------|
| id | info_fk | reg_fk |
| 1 | 100 | 1 |
| 2 | 101 | 1 |
| 3 | 101 | 2 |
| 4 | 102 | 3 |
| 5 | 102 | 4 |
| 6 | 103 | 4 |
|-------------------------------------------|
这个。 reg_fk表示tableRegion的ID,我selectreg_fk基于table信息。 table Mapper 根据 table Info 地址的数量已经存在,所以我所要做的就是阅读Info 的地址并将正确的 ID 放入 Mapper.
这个SQL是我试过的:
with region_info (region_id, info_id) as (
select region.id as region_id, info.id as info_id
from Region region
inner join Info info on
info.address like concat(concat('%', region.region_id), '%')
)
update Mapper mapper inner join Info info
on info_fk = info.id
set mapper.reg_fk = (
case
when info_fk = info.id then
(select region_id
from region_info
where region_info.info_id = info.id
)
end
)
where region_info.info_id = info.id;
它发出错误,我知道我在设置 mapper.reg_fk 时犯了一个错误,有多行我无法将一个 'mapper.reg_fk row' 设置为多行。但是在这个 SQL 句子中,如果我在 info_fk = info.id 上加入信息和映射器,那么我也应该 return 多行。就像在这个例子中一样,info_fk = 100 不会出错,但是 info_fk = 101 然后更新 101 returns 2 行。在 case 语句中,我也得到 2 行(region_id 1 和 2)所以它就像
with join restriction, what to update in table Mapper
|-------------------------------------------|
| id | info_fk | reg_fk |
| 2 | 101 | |
| 3 | 101 | |
|-------------------------------------------|
what appears in case sentence
|--------------------|
| reg_fk |
| 1 |
| 2 |
|--------------------|
我想将 reg_fk 正确更新为 table Mapper。我该怎么做?
在这里您可以简单地构建我的示例数据并测试我的代码(目前无法正常工作)。 http://sqlfiddle.com/#!9/8c0c7d/4
谢谢。
with region_info (region_id, info_id) as (
select region.id as region_id, info.id as info_id
from Region region
inner join Info info on
info.address like concat(concat('%', region.region_id), '%')
)
update Mapper mapper inner join Info info
on info_fk = info.id inner join region_info on info.id = region_info.info_id
set mapper.reg_fk = (
(select region_id
from region_info
where region_info.info_id = info.id
LIMIT 1
)
)
last “其中 region_info.info_id = info.id;”超出子查询。 region_info 在子查询中。所以它找不到 region_info。虽然你用了with子句,但是如果在from子句里,你是找不到的。
您可以使用“LIMIT 1”或“any”(在子查询之前)。