【发布时间】:2018-05-15 13:51:28
【问题描述】:
我正在从各种表中执行SELECT 语句,但我想首先ORDER BY 一组标准。以下是SELECT 语句的示例:
SELECT
a.ItemName,
a.ItemDescription,
a.ItemPrice,
a.DateAdded,
a.UserID,
u.UserType
FROM
table a
inner join user u ON
u.UserID = a.UserID
UNION
SELECT
b.ItemName,
b.ItemDescription,
b.ItemPrice,
b.DateAdded,
b.UserID,
u.UserType
FROM
table b
inner join user u ON
u.UserID = b.UserID
UNION
SELECT
c.ItemName,
c.ItemDescription,
c.ItemPrice,
c.DateAdded,
c.UserID,
u.UserType
FROM
table c
inner join user u ON
u.UserID = c.UserID
从上面的结果集中,我想先ORDER BY where u.UserType = 'Consultant' and DateAdded DESC, and then by u.UserType = 'Internal' and Price DESC and DateAdded DESC。
我怎样才能以上述方式强制排序?
【问题讨论】:
-
附带说明:一个真正的数据库有多个项目表会很奇怪,但这只是我猜的一个例子?真的有重复要删除吗?否则使用
UNION ALL为 DBMS 节省不必要的工作。 -
它们是不同的表,但都包含不同类型的项目。当我在做 UNION 时,我只是给所有列加上别名,以便它们匹配。例如,它实际上是
SELECT c.MonroeProductCode AS ItemDescription等等。
标签: sql sql-server tsql sql-server-2012 sql-server-2014