1

这是我的查询,该查询将首先显示 id 为 2 的用户,然后联合除用户 id 2 之外的其他用户。

(SELECT account.accountid,account.accountname FROM account,entity WHERE entity.id =  account.accountid  AND entity.deleted=0 AND account.accountid = 2) 
UNION 
(SELECT account.accountid,account.accountname FROM account,entity WHERE entity.crmid =  account.accountid  AND entity.deleted=0 AND account.accountid NOT IN(2) ORDER BY account.accountname )

查询很好地显示了结果,但 order by 不起作用。我必须在这里做什么?

请帮我。

4

1 回答 1

1

使用先按account.accountid = 2后按顺序排序的单个查询account.accountname

SELECT account.accountid,account.accountname 
FROM account,entity 
WHERE entity.id =  account.accountid  
AND entity.deleted=0 
order by case when account.accountid = 2 then 1 else 2 end,
         account.accountname 
于 2013-11-05T08:55:03.233 回答