按顺序排序不按正确的顺序显示

时间:2016-05-04 20:13:48

标签: mysql

我有聊天服务,所以:

select k.user, c.id, c.from, c.sent, c.message, c.recd from chat c
inner join cadastro k on c.from=k.id
where (`from`=? and `to`=?)
order by c.id
limit 50

我想显示最近50条消息,如果我发送1到100,它将显示:

1
2
3
...
49
50

我想显示最近50条这样的消息:

50
51
...
100

如果我把order by c.id desc给我订单:

50
49
...
4
3
2
1

任何想法?

2 个答案:

答案 0 :(得分:2)

select * from
(
  select k.user, c.id, c.from, c.sent, c.message, c.recd from chat c
  inner join cadastro k on c.from=k.id
  where (`from`=? and `to`=?)
  order by c.id desc
  limit 50
) tmp
order by id asc

答案 1 :(得分:0)

我不确定这是最有效的方法,但您可以尝试:

SELECT A.user, A.id, A.from, A.sent, A.message, A.recd
FROM (
   select TOP 50 k.user, c.id, c.from, c.sent, c.message, c.recd from chat c
   inner join cadastro k on c.from=k.id
   where (`from`=? and `to`=?)
   order by c.id DESC
) AS A
ORDER BY c.id ASC