2015-08-31 30 views
0

可以說用戶選擇了40的fkey。我想返回與col1,col2組合相匹配的行。基於2個因素的SQL返回行(Oracle/JPA)

Table 1 

id  col1  col2  fkey 
---  ----  --- ---- 
1  A   E  10 
2  A   E  20 
3  A   E  40 
4  B   W  50 
5  B   W  99 
6  C   E  12 
7  C   E  43 

結果:

id  col1  col2  fkey 
---  ----  --- ---- 
1  A   E  10 
2  A   E  20 
3  A   E  40 

回答

1

這是你想要的嗎?

select c.* 
from combos c 
where exists (select 1 
       from combos c2 
       where c2.fkey = 40 and 
        c2.col1 = c.col1 and c2.col2 = c.col2 
      ); 
+0

優秀的,久經考驗的... HTTP://sqlfiddle.com/# 9/678231/3 – CodeIt

1
SEL * 
FROM table1 
WHERE (col1,col2) 
IN (SEL col1, col2 
    FROM table1 
    WHERE fkey = 40) 
ORDER BY fkey 
; 
+0

忘了說了JPA不支持多列IN子句。 – CodeIt