1

我必须生成一个 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  

如何将这些多个查询变成一个查询???
提前致谢。

4

2 回答 2

4

使用 wher in 子句将轻松解决您的问题...

IN 运算符

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

select column 1, column2 from table1, table2 where id in (1,2,3,4,5)
于 2012-05-14T10:10:08.833 回答
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'
于 2012-05-14T10:10:54.697 回答