MySQL选择前10个结果准备语句

时间:2014-06-14 14:20:33

标签: java mysql sql

我已将一些数据插入到数据库中,它包含几个分数。

我需要:

将分数排序并提取前10个结果,包括姓名,ID和用户名

我的插入声明:

    // Insert example
    String sql = "insert into ratings (id, username, score, name) values (?, ?, ?, ?)";

    PreparedStatement preparedStatement = connect.prepareStatement(sql);
    preparedStatement.setLong(1, userID);
    preparedStatement.setString(2, username);
    preparedStatement.setString(3, score);
    preparedStatement.setString(4, name);

    preparedStatement.execute();

5 个答案:

答案 0 :(得分:1)

使用它:

select id, username, score, name from ratings order by score desc limit 10

答案 1 :(得分:0)

您是否尝试过以下操作:

SELECT Id, Username, Score, Name FROM RATINGS ORDER BY SCORE DESC LIMIT 10.

有关详细信息,请参阅MySQL Documentation for Select Clause

答案 2 :(得分:0)

尝试这种方式:

String sql = "insert into ratings (id, username, score, name) values (?, ?, ?, ?) order by score desc limit 10";

PreparedStatement preparedStatement = connect.prepareStatement(sql);
preparedStatement.setLong(1, userID);
preparedStatement.setString(2, username);
preparedStatement.setString(3, score);
preparedStatement.setString(4, name);

preparedStatement.execute();

答案 3 :(得分:0)

下一个怎么样?:

  SELECT id, username, score, name 
    FROM ratings 
ORDER BY SCORE DESC 
   LIMIT 10

答案 4 :(得分:0)

SELECT * FROM RATINGS ORDER BY SCORE DESC LIMIT 10

您需要ORDER BY进行排序,然后才需要 DESC按降序排序,最后排序 LIMIT 10只有10个结果。