Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Order By [PHP & MySQL]

  • 16-03-2003 09:59PM
    #1
    Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭


    Hey There,

    Ok i've a search query in mysql and I want to arrange the rows in a certain order.

    Ok the rows contain a field called "package" and the values can be bronze,silver or gold. So i want a way so that all the gold will go to the top of the search and the silver 2nd and gold last.

    So it would be like

    "SELECT * FROM `table` ORDER BY" I dont know how to finished this query so need help.

    Thanks in advance


Comments

  • Registered Users, Registered Users 2 Posts: 68,173 ✭✭✭✭seamus


    Admittedly, I haven't much experience with SQL, but I don't think you can, since, gold comes between silver and bronze, so any attempt to order it would simply put gold in the centre.

    Perhaps two separate statements could fulfill your needs;

    SELECT winner_name, medal FROM myTable WHERE medal='gold' ORDER BY winner_name DESC

    Then half-create your table with these values, and then do the query;

    SELECT winner_name, medal FROM myTable WHERE medal='bronze' OR medal='silver' ORDER BY medal ASC, winner_name DESC

    then fill in the end of your table.

    That just my 2c :)


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Thanks,

    tho i've come up with another method, just call gold 1, silver 2 and bronze 3 and then they will automatically order by that. but i am so damn lazy to go changing to script now. I'll wait till someone complains on the site. I've everything randomised so the search never ends lol. Like always Next 20 same showing up every round hehehe.


  • Registered Users, Registered Users 2 Posts: 912 ✭✭✭chakotha


    It would be quick enough

    run

    update myTable set package=1 where package='Gold';
    update myTable set package=2 where package='Silver';
    update myTable set package=3 where package='Bronze';

    from phpMyAdmin or shell or script and change form to

    <select name=package>
    <option value=0>Choose
    <option value=1>Gold
    <option value=2>Silver
    <option value=3>Bronze
    </select>

    and

    <?
    $sql="select * from myTable where [whatever] order by package";
    ...
    ?>

    Then there'e edit form tho...


Advertisement