用于检索具有不同where子句的数据的单个SQL查询?

时间:2012-05-14 10:08:07

标签: sql db2

我必须生成一个单独的SQL查询,它从两个表中检索两列 在where子句中,id为1,2,3,4 and 5 如何使用上述指定条件形成单个查询 我只需要sql查询但不需要游标或函数概念 例如;

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  

如何将这些多个查询转换为单个查询? 在此先感谢。

2 个答案:

答案 0 :(得分:4)

利用where子句将轻松解决您的问题...

IN运算符

IN运算符允许您在WHERE子句中指定多个值。

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

答案 1 :(得分:2)

如果查询在每种情况下都相同,只需使用“in”

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

您还可以在其他情况下使用OR:

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