以下代码适用于this fiddle
中显示的Oracle版本> = 9iSELECT T1.col1, T1.col3,count(T2.col1)
FROM T1
LEFT JOIN T2
ON T1.col3 = T2.col3
AND ',' || T2.col2 || ',' like '%,' || T1.col1 || ',%'
GROUP BY T1.col1, T1.col3
但根据Oracle Left Outer Join文章,LEFT JOIN
似乎无法使用Oracle 8i
- 而且不幸的是我在8i工作。
对于=
运算符,我知道等效代码
SELECT T1.col1, T1.col3
FROM T1
, T2
WHERE T1.col3 = T2.col3 (+)
--AND ',' || T2.col2 || ',' like '%,' || T1.col1 || ',%'
问题
但是我们如何为LIKE
运算符指定OUTER JOIN条件。
注意:我相信,如果没有(+)
,它将表现为该列的INNER JOIN。
更新
以下查询会出现ORA-00920: invalid relational operator
错误
SELECT T1.col1, T1.col3,count(T2.col1)
FROM T1
,T2
WHERE T1.col3 = T2.col3(+)
AND (',' || T2.col2 || ',') LIKE ('%,' || T1.col1 || ',%')(+)
GROUP BY T1.col1, T1.col3
答案 0 :(得分:1)
您可以尝试创建包含已修改列的子查询,然后按以下方式连接表:
Select Mt1.T1_Col1, Mt1.T1_Col3,Count(Mt2.T2_Col1)
From
(Select T1.Col1 As T1_Col1, T1.Col3 As T1_Col3, '%,' || T1.Col1 || ',%' As T1_Col1_New From T1) Mt1,
(Select T2.Col1 As T2_Col1, T2.Col2 As T2_Col2, T2.Col3 As T2_Col3, ',' || T2.Col2 || ',' As T2_Col2_New From T2) Mt2
Where Mt1.T1_Col3 = Mt2.T2_Col3 (+)
And Mt1.T1_Col1_New Like Mt2.T2_Col2_New (+)
Group By Mt1.T1_Col1, Mt1.T1_Col3;
我在这里组合了内联子查询和外连接。
参考文献:
答案 1 :(得分:0)
以下是来自fiddle
的代码Select Mt1.T1_Col1, Mt1.T1_Col3,Count(Mt2.T2_Col1)
From
(Select T1.Col1 As T1_Col1,
T1.Col3 As T1_Col3,
'%,' || T1.Col1 || ',%' As T1_Col1_New
From T1) Mt1
,
(Select T2.Col1 As T2_Col1,
T2.Col2 As T2_Col2,
T2.Col3 As T2_Col3,
',' || T2.Col2 || ',' As T2_Col2_New
From T2) Mt2
WHERE Mt1.T1_Col3 = Mt2.T2_Col3 (+)
And Mt2.T2_Col2_New (+) LIKE Mt1.T1_Col1_New
--Mt2 should be on the left side of LIKE
Group By Mt1.T1_Col1, Mt1.T1_Col3;
答案 2 :(得分:-1)
我知道你不想听到这个,但是一旦有人说他们使用的是Oracle< 10g的版本,他们要问的任何问题的答案都是“升级”。在2014年,完全没有理由使用Oracle 8。