user1604294
user1604294

Reputation:

Subquery in the where clause

Hard to google this one, bear with me...looking for rarer syntax. Is there a query like this:

SELECT x
FROM foo 
WHERE foo.y = (foo.a = 3 AND foo.b = 5)

basically the expression within the parens evaluates to a boolean, and I wonder if that can be used to lookup the y column (which stores a boolean values). This syntax is probably wrong, does someone know if there is something like this that exists?

Upvotes: 1

Views: 52

Answers (2)

Muhammad Waheed
Muhammad Waheed

Reputation: 1088

You can use IFF(),using SQL Server, as following:


select x 
from foo
where foo.y = (select iif(foo.a=3 and foo.b=5,0,1));

DB Fiddle Demo

Upvotes: 0

Egan Wolf
Egan Wolf

Reputation: 3573

You can use CASE expression like this:

SELECT x
from foo 
where foo.y = (CASE WHEN (foo.a = 3 AND foo.b = 5) THEN 1 ELSE 0 END)

Upvotes: 1

Related Questions