Solved SQL top 3

Discussion in 'Plugin Development' started by CraftBang, Jun 25, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    CraftBang

    Hey there! I'm using a SQL database but I'm trying to get the top 3 kills (from players)

    I got a table : PlayerStats
    In the table Columns : playername, kills, deaths, mobkills, killstreak

    I'm trying to get the top 3 (highest value) from the column kills.
    I was searching on google and I found this.
    SELECT * FROM `PlayerStats` WHERE kills in (select distinct top 3 kills from PlayerStats order by kills desc)

    But what do I do to receive the 3 kills than as strings? and does it even work what's above?

    Thanks in advance,
    CB
     
  2. Offline

    mythbusterma

    You would probably want:

    Code:
    SELECT name,kills FROM playerstats ORDER BY kills DESC
    And then iterate over the first three results.
     
  3. Offline

    CraftBang

    mythbusterma I just got a question what will this query return?
    and name must be playername I think?
    Thanks already, I'll try this so I'll get this code.
    Code:
    PreparedStatement sql = connection.prepareStatement("SELECT name,kills FROM playerstats ORDER BY kills DESC)");//
    ResultSet resultSet = sql.executeQuery();
    
    What should I do with the resultSet than ?
    Sorry, I don't know a lot about this sql..
     
  4. Offline

    mythbusterma

    You should iterate over the results using result.next() (which returns a boolean of whether or not there is a next, nad increments the resultset) inside of a for loop, and then use resultSet.getInt("kills") or resultSet.getString("name") to get the respective values.
     
    CraftBang likes this.
  5. Offline

    CraftBang

Thread Status:
Not open for further replies.

Share This Page