mysql按查询顺序在联合内部不起作用

时间:2013-11-05 08:53:49

标签: mysql

这是我的查询,查询将向用户显示哪个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 )

查询显示结果非常好但订单依据不起作用。 我在这里要做什么?

请帮帮我。

1 个答案:

答案 0 :(得分: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