dongrong9053 2019-07-22 19:51
浏览 166

如何使用PHP构建SQL查询以对数据库中的多行进行排序和选择

I'm building a game using a platform called Unity. The game integrates with some php scripts and a database I have. Players can register in the game and all info gets stored in the database, including their high score.

I'm trying to write php code that will query the database and complete the following action

*Sort the table by high score

*Return the "username" and "high score" values for the top 25 entries

I don't know how to construct a query to do this. Below is a screen cap of my table and a sample php script of a query used to log in

database of users and high scores

//php below, script checking that there is only one instance of the username

$namecheckquery = "SELECT username, totalpoints, highscore FROM players WHERE username='" . $username . "'"; 

$namecheck = mysqli_query($con, $namecheckquery) or die("2: Name check query failed"); //error code #2 - name check query failed
if (mysqli_num_rows($namecheck) != 1)
{
    echo "5: Either no user with name or more than one"; //error code #5: number of names matching does not = 1 
    exit();
}

$existinginfo = mysqli_fetch_assoc($namecheck);
  • 写回答

1条回答 默认 最新

  • duan0427 2019-07-22 20:05
    关注

    You need ORDER BY {column_name} {ASC|DESC} (ascending by default), and then LIMIT BY {offset}, {length}:

    SELECT username, hightscore
    FROM players
    ORDER BY highscore DESC
    LIMIT 0, 25
    

    You can also add multiple columns to order by, so in case of equality the next column will be used, like:

    SELECT username, hightscore
    FROM players
    ORDER BY highscore DESC, totalpoints DESC
    LIMIT 0, 25
    
    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题