Hi, I have a database table with this structure: id username balance visits referrals I want to select the username and the value of: - The highest balance - The highest value of visits - The highest amount of referrals Example: +---+----------+--------+------+--------+ | id | username | balance | visits | referrals| +---+----------+--------+------+--------+ | 1 | test1 | 100 | 10 | 15 | | 2 | test2 | 50 | 15 | 20 | | 3 | test3 | 25 | 11 | 25 | +---+---------+---------+------+--------+ The query needs to give these results: Highest balance: 100 (username: test1) Highest value of visits: 15 (username: test2) Highest amount of referrals: 25 (username: test3) Thanks in advance for your help
I'm assuming that you are using mySQL. "SELECT SELECT DISTINCT username FROM table ORDER BY balance DESC LIMIT 1 AS top_balance, SELECT DISTINCT username FROM table ORDER BY visits DESC LIMIT 1 AS top_visits, SELECT DISTINCT username FROM table ORDER BY referrals DESC LIMIT 1 AS top_referrals"; Code (markup): You should be able to access the top_balance, top_visits, top_referrals variables in an array by runnning this as a single query. There's probably about 50 other ways to do this as well.