0
我不太確定如何正確地說出這一點。我有兩個表匹配多個列,但在其他列中有不同的值。我需要查詢選擇一個匹配對。我已經包含了一些示例sql來測試。SQL如何使1to1匹配加入多種可能性
CREATE TABLE TEST1
(
LVL1 NUMBER,
LVL2 NUMBER,
LVL3 NUMBER,
DESCR VARCHAR2(30 BYTE)
);
CREATE TABLE TEST2
(
LVL1 NUMBER,
LVL2 NUMBER,
LVL3 NUMBER,
CID NUMBER
);
insert into test1 (lvl1, lvl2, lvl3, descr) values (101, 1, 1, 'lackadaisical');
insert into test1 (lvl1, lvl2, lvl3, descr) values (101, 1, 1, 'martially');
insert into test1 (lvl1, lvl2, lvl3, descr) values (101, 1, 2, 'brian');
insert into test1 (lvl1, lvl2, lvl3, descr) values (101, 2, 1, 'symploce');
insert into test1 (lvl1, lvl2, lvl3, descr) values (101, 2, 2, 'prismatoid');
insert into test1 (lvl1, lvl2, lvl3, descr) values (101, 2, 3, 'delirious');
insert into test1 (lvl1, lvl2, lvl3, descr) values (102, 1, 1, 'discontinuous');
insert into test1 (lvl1, lvl2, lvl3, descr) values (102, 2, 1, 'subseptate');
insert into test1 (lvl1, lvl2, lvl3, descr) values (102, 2, 1, 'heterodox');
insert into test1 (lvl1, lvl2, lvl3, descr) values (102, 2, 1, 'liege');
insert into test1 (lvl1, lvl2, lvl3, descr) values (103, 1, 1, 'mobbish');
insert into test2 (lvl1, lvl2, lvl3, cid) values (101, 1, 1, 10);
insert into test2 (lvl1, lvl2, lvl3, cid) values (101, 1, 1, 15);
insert into test2 (lvl1, lvl2, lvl3, cid) values (101, 1, 2, 20);
insert into test2 (lvl1, lvl2, lvl3, cid) values (101, 2, 1, 25);
insert into test2 (lvl1, lvl2, lvl3, cid) values (101, 2, 2, 30);
insert into test2 (lvl1, lvl2, lvl3, cid) values (101, 2, 3, 35);
insert into test2 (lvl1, lvl2, lvl3, cid) values (102, 1, 1, 40);
insert into test2 (lvl1, lvl2, lvl3, cid) values (102, 2, 1, 45);
insert into test2 (lvl1, lvl2, lvl3, cid) values (102, 2, 1, 50);
insert into test2 (lvl1, lvl2, lvl3, cid) values (102, 2, 1, 55);
insert into test2 (lvl1, lvl2, lvl3, cid) values (103, 1, 1, 60);
因此,每個表包含11個匹配lvl1,lvl2和lvl3的行。我需要查詢返回11行。無論哪個descr與哪個cid匹配都沒有關係,但可以根據其他列設置優先級。
運行下面的查詢將返回19行,每個lvl1,lvl2,lvl3匹配的每個descr和cid組合的可能性都會返回。
select
t1.lvl1,
t1.lvl2,
t1.lvl3,
t1.descr,
t2.cid
from
test1 t1,
test2 t2
where
t2.lvl1 = t1.lvl1 and
t2.lvl2 = t1.lvl2 and
t2.lvl3 = t1.lvl3;
LVL1 LVL2 LVL3 DESCR CID
---------- ---------- ---------- ------------------------------ ----------
101 1 1 martially 10
101 1 1 lackadaisical 10
101 1 1 martially 15
101 1 1 lackadaisical 15
101 1 2 brian 20
101 2 1 symploce 25
101 2 2 prismatoid 30
101 2 3 delirious 35
102 1 1 discontinuous 40
102 2 1 liege 45
102 2 1 heterodox 45
102 2 1 subseptate 45
102 2 1 liege 50
102 2 1 heterodox 50
102 2 1 subseptate 50
102 2 1 liege 55
102 2 1 heterodox 55
102 2 1 subseptate 55
103 1 1 mobbish 60
19 rows selected
編輯 我不能有任何重複無論是在DESCR或每場比賽CID列。例如lvls 101-1-1有2個cid匹配(10,15)到2 descr(懶惰,在軍事上)我需要2行101-1-1,每個descr配對; (缺乏,10和武力,15)或(缺乏,15和武術,10)。
這非常接近,但我沒有在我的問題中指定我不能在每個匹配的descr或cid列中有任何重複項。例如lvls 101-1-1有2個cid匹配('10,15')到2 descr('lackadaisical,martially')我需要2行101-1-1,每個descr配對; ('懶惰,10'和'武術,15')或('懶惰,15'和'武術,10')。 – zerokurns