2011-12-08 244 views
0

有一個databese表;Pl/Sql select語句?

> == id || customer_number || account_type || balance 
> == 1 - 123456   - 1    - 100 
> == 2 - 123457   - 1    - 200 
> == 3 - 123456   - 3    - 200 
> == 4 - 123456   - 4    - 220 
> == 5 - 123456   - 5    - 250 
> == 6 - 123457   - 2    - 200 

我怎麼能選擇具有類型中的至少一個客戶{1和5}?

回答

7

如果你希望客戶與1型或5的賬戶:

SELECT * FROM customers WHERE account_type IN (1, 5); 

[編輯]如果你想客戶Type 1和5的賬戶:

SELECT DISTINCT(c1.customer_number) 
FROM customers c1, customers c2 
WHERE c1.customer_number = c2.customer_number 
    AND c1.account_type = 1 
    AND c2.account_type = 5 
+0

但是如果客戶沒有5,我只想看看結果集中同時包含1和5的類型。 – theklc

+0

更新了我的答案 – socha23

2

你的意思是這樣嗎?

select * from table where account_type IN (1,5); 
0

這裏有一種方法:

SELECT customer_number 
FROM customers c 
WHERE EXISTS 
    (
    SELECT 1 FROM customers c1 WHERE c1.account_type = 1 AND c1.customer_number = c.customer_number) 
    ) 
AND c.account_type = 5