如何组合两个SQL查询

时间:2011-06-06 16:11:31

标签: sql sql-server-2008

如何将两个sql查询合并为一个:

strQuery = "select * from UNITHD where driver1medical BETWEEN '1/1/1990' and GETDATE()+29 ORDER BY driver1medical"
objMedicalDriver1.Open strQuery

strQuery = "select * from UNITHD where driver2medical BETWEEN '1/1/1990' and GETDATE()+29 ORDER BY driver1medical"
objMedicalDriver2.Open strQuery

我确实在stackoverflow找到了很多例子但是我无法让它工作: - (

2 个答案:

答案 0 :(得分:4)

select * from UNITHD
where ( driver1medical BETWEEN '1/1/1990' and GETDATE()+29 )
   or ( driver2medical BETWEEN '1/1/1990' and GETDATE()+29 )
ORDER BY driver1medical

答案 1 :(得分:1)

这样它应该列出所有结果组合,记住要使用UNION操作数,你需要将*替换为两个查询中的属性列表,以便它们匹配(在数量和域中):

select * from UNITHD where driver1medical BETWEEN '1/1/1990' and GETDATE()+29 ORDER BY driver1medical

UNION

select * from UNITHD where driver2medical BETWEEN '1/1/1990' and GETDATE()+29 ORDER BY driver1medical