简体   繁体   中英

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} ?

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

你的意思是这样吗?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM