DESC的SQL命令无法正常工作

时间:2014-06-10 15:03:21

标签: sql

我正在使用子查询进行相对较小的查询,其中我想按降序对子查询结果进行排序。但是在结果中,子查询不按降序排序。看不出ORDER BY对我不起作用的原因......

我的疑问:

select 

customers.id,
customers.Name,
customers.Surname,

(select ifnull(sum(bets.amount),0)
from bets 
where customers.id=bets.customerId
and bets.date >'2014-06-01'
and bets.date <'2014-06-02'
order by bets.amount DESC
) as '1st_June',

(select ifnull(sum(bets.amount),0)
from bets 
where customers.id=bets.customerId
and bets.date >'2014-06-02'
and bets.date <'2014-06-03'
order by bets.amount DESC
) as '1st_June',


from customers

group by customers.id

我需要有一个DESC订单,因为我想要限制100,所以我得到前100个值。有人可以建议这样做吗??

1 个答案:

答案 0 :(得分:1)

Order By并没有像那样继续过去。

这应该会为您提供您正在寻找的内容:

SELECT customers.id
    ,customers.Name
    ,customers.Surname
    ,(
        SELECT ifnull(sum(bets.amount), 0)
        FROM bets
        WHERE customers.id = bets.customerId
            AND bets.date > '2014-06-01'
            AND bets.date < '2014-06-02'
        ) AS First_June
    ,(
        SELECT ifnull(sum(bets.amount), 0)
        FROM bets
        WHERE customers.id = bets.customerId
            AND bets.date > '2014-06-02'
            AND bets.date < '2014-06-03'
        ) AS Second_June
FROM customers
GROUP BY customers.id
ORDER BY First_June DESC
    ,Second_June DESC
LIMIT 100
  • 注意:(更改为&#34; First_June&#34;相反。使用数字开始列名在多个SQL Server中存在问题,并且您没有指定您正在使用的内容.Oracle, MS SQL,MySql,Postgres等)