mysql按字母排序输出但得到ASC?

时间:2011-12-09 13:28:22

标签: php mysql

我有一个mysql语句:

SELECT userid,username FROM table1 ORDER BY userid DESC LIMIT 15

我想要做的是从表中获取最后15个,但是也按字母顺序对用户名进行排序以便输出。

这样做的语法是什么?

5 个答案:

答案 0 :(得分:3)

您可以像这样使用subquery: -

select userid, username
from 
(
  SELECT userid,username FROM table1 ORDER BY userid DESC LIMIT 15
) as last_15
order by username;

答案 1 :(得分:0)

这样的事情怎么样:

  

SELECT username,userid FROM table1 WHERE userid IN(SELECT userid   FROM table1 ORDER BY userid DESC LIMIT 15)ORDER BY username ASC

答案 2 :(得分:0)

您可以将原始SELECT换成新的

SELECT userid,username FROM (
    SELECT userid,username FROM table1 ORDER BY userid DESC LIMIT 15
) ORDER BY username ASC

或者由于数据非常简单,您可以将原始结果加载到PHP中的数组中并对数组进行排序。

答案 3 :(得分:0)

你可以尝试这个

SELECT userid,username FROM table1 ORDER BY username ASC LIMIT 15,0

答案 4 :(得分:0)

select  last_15.userid,  last_15.username
from 
(
  SELECT userid,username FROM table1 ORDER BY userid DESC LIMIT 15
) as last_15
order by last_15.username;

此查询甚至可以快速运行