Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Using "OR" in WHERE section of SELECT statement

Hello,

I'm trying to write a SELECT statement where the recordset returned will match 1 of 2 conditions, but not both! When I use "SELECT * FROM <table> WHERE ((columnA = x) or (columnA = 0)), I'll get records where columnA = x and columnA = 0. What I'm hoping for is get records where columnA = x and if that count = 0, get records where columnA = 0.

My database is MS SQL 2000.

Any help would be greatly appreciated!
[468 byte] By [skipperben] at [2007-11-11 10:08:47]
# 1 Re: Using "OR" in WHERE section of SELECT statement
I don't think you can do that with a simple SELECT. Try something like this:

if (select count(*) from <table> where columnA = x) > 0
select * from <table> where columnA = x
else
select * from <table> where columnA = 0
Phil Weber at 2007-11-11 23:43:26 >
# 2 Re: Using "OR" in WHERE section of SELECT statement
This is rather idle (as the posted code does solve the problem equally well).

However, you can do it in a single select clause (although a compound one). However, this query depends on non-standard SQL, so the query would not be portable from one DBMS to another. On the other hand it has the advantage of allowing as much values for columnA as you want (the code posted by Phil would require adding additional if statements for each different value you need to test for in columnA if you need more than two values). It is however not very fast if the inner select fetches many rows (the grouping and ordering could be quite expensive, a distinct clause should work fine, also).

Here's how, in MS SQL idiom:

select * from table
where columnA in (
select top 1 columnA
from table
where columnA in (0,x,y,z)
group by columnA
order by columnA)

For Oracle, notice that the "TOP" clause disappears and is replaced by a "HAVING" condition:

select * from table
where columnA in (
select columnA
from table
where columnA in (0, x, y, z)
group by columnA
order by columnA having rownum < 2)

Yet again, another variation (this time, for PostgreSQL):

select * from table
where columnA in (
select columnA
from table
where columnA in (0, x, y, z)
group by columnA
order by columnA limit 1)

As you see, each DBMS requires a different approach, as not all of them implement the same keywords.

Cheers.
mikkus at 2007-11-11 23:44:27 >