3

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.

1
  • yes you can use where (LastName, FirstName) in ('LN1','FN1') Commented Aug 7, 2015 at 2:07

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

To add Signle Quote helps! I am using DB2, and that two columns i use are 'varchar", so single quote is must. Previously I am using double quote ", which not help. thanks!
Available in Oracle but not SQL Server - see stackoverflow.com/questions/35194028/…
0

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';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.