抱歉,我是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,但是得到一个错误,它们的目标列表中必须具有相等数量的表达式。
任何帮助将不胜感激。
假设数据类型全部排在您的其他列中,则可以在第二个查询中添加一个虚拟列以使UNION
工作。
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))
UNION
select c1,c2,c3,c4,c5,c6, NULL AS c7 --<---- Dummy c7 column added.
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))