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.

2 Select statement into one state

Discussion in 'PHP' started by PinoyIto, Mar 5, 2017.

  1. #1
    I have the following statement
    
    Select person, age from people order by age desc
    Select person, score from people order by score desc
    
    Code (markup):
    Is it possible to put these two query in single select statement?

    Thanks in advance...
     
    PinoyIto, Mar 5, 2017 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Well, yes, but you won't get two distinct order by. You can do an order by age, score, and the result will be ordered by age, and if there is equal age then score. However, depending on how you present the data, you could add sorting options on the data on the page.
     
    PoPSiCLe, Mar 5, 2017 IP
  3. PinoyIto

    PinoyIto Notable Member

    Messages:
    5,863
    Likes Received:
    170
    Best Answers:
    0
    Trophy Points:
    260
    #3
    Data should be look like this
     
    PinoyIto, Mar 5, 2017 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    If you only want to sort on age, then just do
    
    Select person, age, score from people order by age desc
    
    Code (markup):
    Although that there is bad database design. While age can be argued could be part of the person-table, at least the score should be in its own table, linked by unique IDs.
     
    PoPSiCLe, Mar 5, 2017 IP
  5. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #5
    This would sort by age descending first, then score descending second:

    
    SELECT person, age, score FROM people ORDER BY age DESC, score DESC
    
    Code (markup):
     
    ThePHPMaster, Mar 5, 2017 IP
    deathshadow likes this.
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #6
    What he said, and bonus points for following the naming convention of commands in uppercase and fields in lowercase so you can tell what the **** is going on in there.
     
    deathshadow, Mar 8, 2017 IP
    mmerlinn likes this.