SQL通过将表1与表2中的2个条件匹配来选择表列

Posted

技术标签:

【中文标题】SQL通过将表1与表2中的2个条件匹配来选择表列【英文标题】:SQL select table columns by matching Table 1 with 2 conditions in Table 2 【发布时间】:2020-07-11 15:17:59 【问题描述】:

我想从具有以下条件的两个表中检索匹配项:

    从 Table_A 和 Table_B 中获取列,其中 A.some_name = B.j_name
Table_A:
**AB  some_name     G_NAME      Status        some_time**
------------------------------------------------------------
AAA    Job1        xxxxxxxxx   Ended OK    2020-06-29 10:37:52
AAA    Job2        xxxxxxxxx   Ended OK    2020-06-29 10:37:52
BBB    AB-Job1     xxxxxxxxx   Ended OK    2020-06-29 10:37:52
BBB    AB-Job2     xxxxxxxxx   Ended OK    2020-06-29 10:37:52
BBB    AB-Job3     xxxxxxxxx   Ended OK    2020-06-29 10:37:52
Table_B:
**RM  j_name           desc            rand_time**
----------------------------------------------------
111   Job1            Sometext    2020-06-29 06:30:51
111   AB-Job1         Sometext1   2020-06-29 09:31:52
222   AB-Job5         Sometext2   2020-06-29 09:34:11
222   DPF-AB-Job2     Sometext3   2020-06-29 03:39:33
222   DPF-AB-Job3     Sometext4   2020-06-29 11:32:23
SELECT a.some_name, a.some_time ,b.desc
FROM TableA a
LEFT JOIN Table_B b
ON a.some_name = b.j_name 
where a.some_name like 'AB-%'

现在我想从 Table_A 中选择 AB-Job2 and AB-Job3 并在同一个 select 语句中将其与 Table_B 的 DPF-AB-Job2 and DPF-AB-Job3 匹配。 我将如何做到这一点?

【问题讨论】:

所以你想要两个数据集?我的意思是,第一个检索匹配条件 a.some_name = b.j_name ,第二个检索匹配另一个条件。都在同一个语句中? 是的@RobertoHernandez 【参考方案1】:

使用IN()匹配多个值,并使用字符串连接生成其他可能的值。

SELECT *
FROM TableA a
LEFT JOIN Table_B b
ON b."j_name" IN (a."some_name", 'DPF-' || a."some_name")
where a."some_name" like 'AB-%';

DEMO

【讨论】:

谢谢...试过了..但不幸的是,它在 desc 列@Barmar 中给了我一个空值 我的情况倒退了。我已经修复它并添加了一个现场演示。【参考方案2】:

考虑到您想要保留两个数据集并且您正在进行左连接,然后

SQL> create table table_a ( code varchar2(3) , name varchar2(10) , g_name varchar2(100) default 'xxxxxxxxx' ) ;

SQL> create table table_b ( othercode varchar2(3) , name varchar2(20) , description varchar2(40) default 'yyyyyyy' )
  2  ;

为示例插入一些记录后

SQL> select * from table_a ;

COD NAME       G_NAME
--- ---------- --------------------
AAA Job2       xxxxxxxxx
AAA AB-Job1    xxxxxxxxx
AAA AB-Job2    xxxxxxxxx
AAA AB-Job3    xxxxxxxxx

SQL> select * from table_b ;

OTH NAME                 DESCRIPTION
--- -------------------- ----------------------------------------
111 Job1                 yyyyyyy
112 AB-Job1              yyyyyyy
113 AB-Job5              yyyyyyy
114 DPF-AB-Job2          yyyyyyy
115 DPF-AB-Job3          yyyyyyy

SQL>

现在,如果我们运行您的查询:

SQL> SELECT a.code, b.othercode, a.name, b.name ,b.description
FROM table_a a
LEFT JOIN table_b b
ON a.name = b.name
where a.name like 'AB-%'  2    3    4    5  ;

COD OTH NAME       NAME                 DESCRIPTION
--- --- ---------- -------------------- ----------------------------------------
AAA 112 AB-Job1    AB-Job1              yyyyyyy
AAA     AB-Job2
AAA     AB-Job3

SQL>

您从 table_b 中获取所有符合条件的记录,以及从 table_a 中不匹配的所有记录。如果您还想要与其他条件匹配的记录,您可以使用 UNION。

SELECT a.code, b.othercode, a.name, sysdate ,b.description
FROM table_a a
LEFT JOIN table_b b
ON a.name = b.name
where a.name like 'AB-%'
union
SELECT a.code, b.othercode, a.name, sysdate ,b.description
FROM table_a a
LEFT JOIN table_b b
ON a.name = replace(b.name,'DPF-','')
where a.name like 'AB-%'

COD OTH NAME       SYSDATE   DESCRIPTION
--- --- ---------- --------- ----------------------------------------
AAA 112 AB-Job1    11-JUL-20 yyyyyyy
AAA 114 AB-Job2    11-JUL-20 yyyyyyy
AAA 115 AB-Job3    11-JUL-20 yyyyyyy
AAA     AB-Job2    11-JUL-20
AAA     AB-Job3    11-JUL-20

在这种情况下,您得到了两个数据集,但由于左连接,您得到了描述空值。正常内部连接的结果差异会告诉您

 SELECT a.code, b.othercode, a.name, sysdate ,b.description
  2  FROM table_a a
  3  JOIN table_b b
  4  ON a.name = b.name
  5  where a.name like 'AB-%'
  6  union
  7  SELECT a.code, b.othercode, a.name, sysdate ,b.description
  8  FROM table_a a
  9  JOIN table_b b
 10  ON a.name = replace(b.name,'DPF-','')
 11* where a.name like 'AB-%'
SQL> /

COD OTH NAME       SYSDATE   DESCRIPTION
--- --- ---------- --------- ----------------------------------------
AAA 112 AB-Job1    11-JUL-20 yyyyyyy
AAA 114 AB-Job2    11-JUL-20 yyyyyyy
AAA 115 AB-Job3    11-JUL-20 yyyyyyy

SQL>

如有任何疑问,请随时提出 希望对你有帮助

【讨论】:

感谢@Roberto Hernandez 的详细回答!但奇怪的是,当我在更大的数据库上运行它时,我似乎只能得到有限的数据。 你能举一个你应该得到但你没有得到的行的例子吗? 其实重新查了一下,用order by按时间降序排序,然后看到了我要找的记录。所以是的,你的解决方案也有效。谢谢@Roberto Hernandez!

以上是关于SQL通过将表1与表2中的2个条件匹配来选择表列的主要内容,如果未能解决你的问题,请参考以下文章

如何用sql语句,将表2中与表1中含有相同字符行,中的数据插入表1中

SQL Server 外部应用查询优化

从 2 个表中获取数据

将选定日期与表列或 MySQL 查询生成的日期数组进行比较

如何对 BigQuery 中的两个表进行条件连接?

如何将表列从十进制更改为 varchar [重复]