如何合并两个查询的结果

时间:2020-03-23 19:20:04

标签: sql sql-server tsql

对不起,但是我是SQL查询的新手,可能需要一点帮助。

我想从2个表中加入结果,但似乎无法得到它返回的结果。

查询1

select c1,c2,c3,c4,c5,c6,c7
from t1
where (c3 between cast(dateadd(day, -1,Getdate()) AS Date and cast(dateadd(day,0,getdate()) as date))

查询2

select c1,c2,c3,c4,c5,c6
from t2
where c2 in ('user', 'connected', 'agent') and
(c3 between cast(dateadd(day, -1,Getdate()) AS Date and cast(dateadd(day,0,getdate()) as date))

我尝试使用union all,但是得到一个错误,即它们的目标列表中必须具有相等数量的表达式。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

使用联合时,两个查询在select语句中必须具有相同数量的字段。查询1有7个项目,而查询2有6个项目。您可以向查询2添加空值。