问题
I would like to retrieve matches from two tables with below criteria:
- Fetch columns from Table_A and Table_B where 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-%'
Now I'd like to select AB-Job2 and AB-Job3
from Table_A and match it with DPF-AB-Job2 and DPF-AB-Job3
of Table_B too in the same select statement.
How would I accomplish this?
回答1:
Use IN()
to match multiple values, and use string concatenation to generate the other possible value.
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
回答2:
Keeping in consideration you want to keep both datasets and that you are making a left join, then
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 ;
After inserting some records for the example
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>
Now, if we run your query:
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>
you get all records from table_b which match the condition, plus all records from table_a which don't. As you want also the records that match the other condition you can use 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
In this case, you got both datasets, but you got description nulls because of the left join. A difference of results with a normal inner join will show you that
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>
Feel free to ask any doubt you might have Hope it helps
来源:https://stackoverflow.com/questions/62850965/sql-select-table-columns-by-matching-table-1-with-2-conditions-in-table-2