Sort table by reputation and select rank + LIMIT to print above and below rows <p>I am trying to sort the table by the reputation and then select the position of the row [rank], it works fine, but I would like to print out 3 tables above and 3 tables below this rank row, but LIMIT rank,6 does not work :/ (Im sorry for that wrong formatted SQL :/) Would be thankful for every help :)</p> <p>Here is what is working:</p> <pre><code>$query = mysqli_query($con, "SET @rank=0"); $query = mysqli_query($con, " SELECT rank, user_email, reputation FROM ( SELECT @rank:=@rank+1 AS rank, user_email, reputation FROM accounts, (SELECT @rank := 0) r ORDER BY reputation DESC LIMIT 0,7 ) t WHERE reputation &gt;= '5' OR reputation &lt; '5'"); </code></pre> <p>prints </p> <pre><code>[rank] [user_email] [reputation] 1 mail1@gmail.com 20 2 test@test.com 15 3 mail2@gmail.com 10 4 othermail@gmail.com 5 5 hmmmmm@gmail.com 0 6 ouch@gmail.com 0 7 somemail@gmail.com 0 </code></pre>