Mr.Chowdary
Mr.Chowdary

Reputation: 507

Single SQL query for retrieval of data with different where clause possible?

I have to generate a single sql query, which retrieves two columns from two tables.
In where clause the id is 1,2,3,4 and 5.
How to form a single query with the above specified condition.
I need only sql query but not cursor or functions concept.
Eg;

select column 1, column2 from table1, table2 where id=1  
select column 1, column2 from table1, table2 where id=2  
select column 1, column2 from table1, table2 where id=3  
select column 1, column2 from table1, table2 where id=4  

How to make these multiple queries into a single query???
Thanks in Advance.

Upvotes: 1

Views: 978

Answers (2)

Stefan Steinegger
Stefan Steinegger

Reputation: 64628

If the query is the same in each case, just use "in"

select column 1, column2 from table1, table2 where id in (1, 2, 3, 4, 5)

You can also use OR in other cases:

select column 1, column2 from table1, table2 where id = 1 OR name = 'XY'

Upvotes: 2

Pranay Rana
Pranay Rana

Reputation: 176896

make use of wher in clause will resolve your issue easily ...

The IN Operator

The IN operator allows you to specify multiple values in a WHERE clause.

select column 1, column2 from table1, table2 where id in (1,2,3,4,5)

Upvotes: 4

Related Questions