简体   繁体   English

SQL 查询在一个表中搜索记录并将其替换为另一个表中的多条记录

[英]SQL query to search for a record in one table and replace it with multiple records from another table

I have two tables - package and subpackage as:我有两个表 - 包和子包为:

CREATE TABLE Package (
    Sequence int PRIMARY KEY,
    Package int,
    Mnemonic char(3),
    MnemonicValue int
);

CREATE TABLE SubPackage (
    Sequence int PRIMARY KEY,
    SubPackage int,
    Mnemonic char(3),
    MnemonicValue int
);

INSERT INTO Package (Sequence, Package, Mnemonic, MnemonicValue)
VALUES (1, 111, 'XXX', 0), (2, 111, 'SUB', 153), (3, 111, 'DDD', 30), (4, 111, 'YYY', 20), (5, 111, 'ZZZ', 1000);

INSERT INTO SubPackage (Sequence, SubPackage, Mnemonic, MnemonicValue)
VALUES (1, 153, 'AAA', 20), (2, 153, 'BBB', 1000), (3, 153, 'CCC', 30);

The requirement is to search for a Mnemonic 'SUB' in the package table and replace the SUB record with the records from 'Subpackage' table for the mapped Mnemonic Value (153) and resequence the sequence values as shown in the [Result][2] table.要求是在包表中搜索助记符 'SUB' 并将 SUB 记录替换为映射助记符值 (153) 的 'Subpackage' 表中的记录,并对序列值重新排序,如 [Result][2] ] 桌子。

I have tried using the update statement as:我曾尝试将更新语句用作:

UPDATE package
SET    Mnemonic = subpackage.Mnemonic
FROM   package
       INNER JOIN subpackage
       ON package.MnemonicValue = subpackage.SubPackage

But this replaces the SUB record from package table with only AAA record from subpackage table.但这只是用子包表中的 AAA 记录替换了包表中的 SUB 记录。 I want SUB record to be replaced with all records from subpackage table and re-sequence it in a new table as shown in the Result table.我希望将 SUB 记录替换为子包表中的所有记录,并在新表中对其重新排序,如结果表中所示。

Desired result:想要的结果:

Sequence | Particular | Mnemonic | MnemonicValue

       1 |         111|       XXX|             0        
       2 |         111|       AAA|            20
       3 |         111|       BBB|          1000
       4 |         111|       CCC|            30
       5 |         111|       DDD|            30
       6 |         111|       YYY|            20
       7 |         111|       ZZZ|          1000

You can use a left join to get the results that you want:您可以使用left join来获得您想要的结果:

select
    p.sequence,
    p.package,
    coalesce(s.mnemonic, p.mnemonic) mnemonic,
    coalesce(s.mnemonic_value, p.mnemonic_value) mnemonic_value
from package p
left join sub_package s 
    on  p.mnemonic = 'SUB'
    and p.mnemonic_value = s.sub_package

"Replacing" the new rows is a bit more complicated. “替换”新行有点复杂。 It would probably be simpler to insert the new rows, then remove the original (you can do this in a transaction):插入新行然后删除原始行可能会更简单(您可以在事务中执行此操作):

start transaction;

insert into package(sequence, package, mnemonic, mnemonic_value)
select
    p.sequence,
    p.package,
    coalesce(s.mnemonic, p.mnemonic),
    coalesce(s.mnemonic_value, p.mnemonic_value)
from package p
left join sub_package s 
    on  p.mnemonic = 'SUB'
    and p.mnemonic_value = s.sub_package;

delete from package where mnemonic = 'SUB';

commit;

I don't think you can update Package table to get exactly result that you want, ie problem is that in your example you reordering Sequence column.我认为您无法更新 Package 表以获得您想要的确切结果,即问题是在您的示例中您重新排序了Sequence列。

Anyway, here is SQL script(based on answer by GMB) that will generate result that you want to get:无论如何,这里是 SQL 脚本(基于 GMB 的回答),它将生成您想要获得的结果:

select
    ROW_NUMBER() OVER (ORDER BY p.Sequence) as Sequence,
    p.Package,
    coalesce(s.Mnemonic, p.Mnemonic) Mnemonic,
    coalesce(s.MnemonicValue, p.MnemonicValue) MnemonicValue
from Package p
left join SubPackage s 
    on p.MnemonicValue = s.SubPackage
order by p.Sequence

But, AFAIK it works only with MySQL 8但是,AFAIK 它仅适用于 MySQL 8

Here is a link to SQLFiddle , but beware it's for Postgres(as SQLFiddle doesn't support MySQL 8).这是SQLFiddle链接,但要注意它是用于 Postgres 的(因为 SQLFiddle 不支持 MySQL 8)。

If you're using earlier version of MySQL, you can try to emulate row_number function如果您使用的是较早版本的 MySQL,您可以尝试模拟 row_number 函数

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL查询从一个表获取所有记录,除特定记录,按日期,从另一个表 - SQL Query To Get All Records From One Table, Except A Specific Record, By Date, From Another Table 在一个查询中从两个表中选择一个记录,并从另一个表中选择多个记录 - Selecting one record from two tables and multiple records from another table in ONE query SQL查询-从一个表中选择全部,在另一个表中匹配记录 - SQL Query - select all from one table with matching records in another SQL 查询返回一个表中的记录,这些记录分别与另一张表中的 2 条记录相关联 - SQL query to return records from one table that are associated to 2 records each from another table SQL在查询结果中替换另一个表中的多个变量 - SQL Replace multiple variables from another table in query result SQL QUERY在一行中多次搜索以从同一表的另一行中查找数据 - SQL QUERY multiple search in one row to find data from another row in the same table SQL用另一个表中的记录更新一个表的记录? - SQL to Update records of one table with records from another table? 查询将一条记录(ID)从一张表插入到另一张表 - Query for insert one record (ID) from one table to another table SQL ..编写查询以匹配一个表中的 name 和 dob 以在另一个表中查找记录 - SQL..Write a query to match on name and dob from one table to find record in another table SQL查询可从一个表中获取所有记录,并在另一个表上进行连接,包括任何其他唯一记录 - SQL Query to get all records from one table and join on another including any additional unique records
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM