Coeus Wang
Coeus Wang

Reputation: 268

SQL where conditions in-list with multiple item?

I would like to query a table, where i need 2 columns meet certain criteria, as an example, a table like below:

LastName FirstName Age address
LN1      FN1        18  ADD1
LN2      FN1        20  ADD2

Now I want to extract 1st address like below:

select * from mytable
where LastName in 'LN1' and FirstName in 'FN1' 

Can I use such a condition:

where (LastName, FirstName) in (LN1,FN1)

please kindly help. thanks.

Upvotes: 3

Views: 7641

Answers (2)

Rakesh_SQL_Specialist
Rakesh_SQL_Specialist

Reputation: 41

You can either use or or and

select * from mytable where LastName = 'LN1' or FirstName = 'FN1';
select * from mytable where LastName = 'LN1' and FirstName = 'FN1';

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1270091

Some databases support the syntax you want:

where (LastName, FirstName) in ('LN1', 'FN1')

Note that the single quotes are important, because they are string constants.

In other databases, you need to do:

where LastName = 'LN1' or FirstName = 'FN1'

or perhaps put the constants into a derived table, and use a join for the matching.

Upvotes: 3

Related Questions