3

I try to get some users which meet certain conditions:

SELECT * FROM users
WHERE lastname IN ('Pitt','Jolie','Costner') 
AND firstname IN ('Brad','Angelina','Kevin')

But this way, Kevin Jolie and Brad Costner will be shown, which is NOT what I need.

So I tried:

SELECT * FROM users
WHERE (lastname,firstname) IN ('Pitt','Brad'),('Jolie','Angelina'),('Costner','Kevin')

Which throws the error:

An expression of non-boolean type specified in a context where a condition is expected, near ','.

Do you know a query that will yield my expected result?

Attn: The two or more columns are not always varchar columns, they may be of any type.

Alexander
  • 19,906
  • 19
  • 75
  • 162

2 Answers2

3

Try this please.

SELECT * FROM users
WHERE lastname+firstname IN ('PittBrad','JolieAngelina','CostnerKevin')

In case the columns have null then

SELECT * FROM users
WHERE ISNULL(lastname,'')+ISNULL(firstname,'') IN ('PittBrad','JolieAngelina','CostnerKevin')
mohan111
  • 8,633
  • 4
  • 28
  • 55
1

I would use a query like this (but i'm using SQL-Server 2005, maybe there's something neater now):

SELECT * FROM users
WHERE 
    (firstname = 'Angelina' AND lastname = 'Jolie') 
OR
    (firstname = 'Brad' AND lastname = 'Pitt') 
OR
    (firstname = 'Kevin' AND lastname = 'Costner') 
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939