1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Select highest values

Discussion in 'MySQL' started by webmasterplace, Apr 28, 2008.

  1. #1
    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 ;)
     
    webmasterplace, Apr 28, 2008 IP
  2. delhi_wala

    delhi_wala Peon

    Messages:
    75
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    One query or one query for each value you want (balance, visits and referrals)?
     
    delhi_wala, Apr 28, 2008 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    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.
     
    jestep, Apr 28, 2008 IP
  4. webmasterplace

    webmasterplace Well-Known Member

    Messages:
    802
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #4
    Thanks for the help!
     
    webmasterplace, Apr 29, 2008 IP