table1
,table2
并且table3
有不同的列,但都有一个OrderDate
列。我想从所有 3 个表中获取一组行的结果,并且我希望最终结果集按OrderDate
.
( select * from table1 LEFT join table2 on 0=1 LEFT join table3 on 0=1
where somedate <= table1.orderdate )
union all
( select * from table1 RIGHT join table2 on 0=1 LEFT join table3 on 0=1
where somedate <= table2.orderdate )
union all
( select * from table1 RIGHT join table2 on 0=1 RIGHT join table3 on 0=1
where somedate <= table3.orderdate )
这可行,但我希望这个结果集按 排序orderdate
,所以我添加:
order by case when table1.orderdate is not null then table1.orderdate
when table2.orderdate is not null then table2.orderdate
else table3.orderdate end
SQL Server 返回错误“如果语句包含 UNION、INTERSECT 或 EXCEPT 运算符,则 ORDER BY 项必须出现在选择列表中。”
如果我更换
select *
经过
select *, table1.orderdate, table2.orderdate, table3.orderdate
我犯了同样的错误。
嗯?谢谢。