我正在使用子查询进行相对较小的查询,其中我想按降序对子查询结果进行排序。但是在结果中,子查询不按降序排序。看不出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个值。有人可以建议这样做吗??
答案 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