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

1tu0hz3e  于 2021-07-24  发布在  Java
关注(0)|答案(2)|浏览(430)

我想用以下条件从两个表中检索匹配项:
从表a和表b中获取列,其中a.some\u name=b.j\u 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-%'

现在我要选择 AB-Job2 and AB-Job3 从表a中选择并与匹配 DPF-AB-Job2 and DPF-AB-Job3 也在同一select语句中。我该如何做到这一点?

uelo1irk

uelo1irk1#

使用 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-%';

演示

imzjd6km

imzjd6km2#

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

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>

您将获得表\u b中符合条件的所有记录,以及表\u 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>

如果有任何疑问,请随时提出,希望能有所帮助

相关问题