John Lucas
John Lucas

Reputation: 37

SQL OR Operator Alternatives

I'm trying to select a table called PRODUCT with and only display the rows where V_CODE = 22000 or 55000. The problem is that I can't use the OR statement for my final answer. Is there another way to remove the OR and get the same result? See below:

SELECT * FROM PRODUCT_revised WHERE V_CODE = 22000 OR V_CODE = 55000;

Upvotes: 0

Views: 1032

Answers (4)

ypercubeᵀᴹ
ypercubeᵀᴹ

Reputation: 115560

Another way besides IN would be to use UNION:

SELECT * FROM PRODUCT_revised WHERE V_CODE = 22000
UNION ALL
SELECT * FROM PRODUCT_revised WHERE V_CODE = 55000;

or be fancy and use De Morgan laws:

SELECT * FROM PRODUCT_revised 
WHERE NOT (NOT (V_CODE = 22000) AND NOT (V_CODE = 55000));

Upvotes: 2

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112437

Use a mathematical trick:

SELECT * FROM PRODUCT_revised WHERE (V_CODE - 22000) * (V_CODE - 55000) = 0;

Upvotes: 3

Gordon Linoff
Gordon Linoff

Reputation: 1270021

The preferred solution is to use in:

SELECT *
FROM PRODUCT_revised
WHERE V_CODE IN (22000, 55000);

However, your code should work.

Upvotes: 2

potashin
potashin

Reputation: 44581

You can use IN :

SELECT * FROM PRODUCT_revised WHERE V_CODE IN(22000, 55000);

Upvotes: 5

Related Questions