Bilgin Kılıç
Bilgin Kılıç

Reputation: 9119

Pl / Sql select statement?

There is an databese table;

> == 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

How can I select the customers that have at least one of the types of {1 and 5} ?

Upvotes: 0

Views: 479

Answers (3)

Kevin Burton
Kevin Burton

Reputation: 11936

Here is one way to do it:

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

Upvotes: 0

socha23
socha23

Reputation: 10239

If you want customers with accounts of type 1 OR 5:

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

[EDIT] If you want customers with accounts of type 1 AND 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

Upvotes: 7

Mithun Sasidharan
Mithun Sasidharan

Reputation: 20920

Did you mean something like this?

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

Upvotes: 2

Related Questions