dongyi5070
2014-06-21 08:39 阅读 8
已采纳

MySQL ORDER BY不是最常见的

Hello i have this table which contains users and the amount of points they have and i want to display the table starting from the users with the highest points but when i try the code below what it does is it shows the most common amount of points.

SELECT * FROM users ORDER BY points ASC

But instead of

User | Points
Bob    20
Jon    16
Abu    16
Eli    15

It does

User | Points
Jon    16
Abu    16
Bob    20
Eli    15

See shouldn't 20 be on top? it does it by most common points any help I am new to PHP

EDIT: Thanks to Hanky 웃 Panky i realized what i needed to do

ORDER BY CAST(points AS unsigned) DESC 

Is the correct way.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    dounao2829 dounao2829 2014-06-21 08:42

    Your Points field must be a String to be interpreted that way. Cast it as a number( if you cant change data type to become a number permanently) and you are good.

    SELECT * FROM users ORDER BY CAST(points AS unsigned) ASC
    

    And for a Descending order, use DESC instead of ASC

    Fiddle

    点赞 评论 复制链接分享
  • doubomudichen0832 doubomudichen0832 2014-06-21 08:42

    use

    QUERY

    SELECT * FROM users ORDER BY points DESC  
    

    for descending order

    点赞 评论 复制链接分享
  • duancao1951 duancao1951 2014-06-21 08:46

    Please, change of type points string to integer or numeric type.

    ALTER TABLE tablename MODIFY columnname INTEGER;
    
    ALTER TABLE users MODIFY points INTEGER;
    
    点赞 评论 复制链接分享

相关推荐