Wickd
Wickd

Reputation: 156

PHP/MySQL - Sorting data starting with specific rows

I have MySQL table with similar data

France  - 1
Germany - 2
Italy   - 3
France  - 5
Germany - 3
France  - 2

I want to select everything (It's easy SELECT * FROM table), but I want to sort data the way that France will be always first, so outcome should be:

France  - 1
France  - 5
France  - 2
Germany - 2
Germany - 3
Italy   - 3

Can this be done on MySQL side or I should do this inside foreach statement?

Thank you.

Upvotes: 1

Views: 97

Answers (2)

Vipin
Vipin

Reputation: 153

you can also do this by

$result=mysql_query("select * from   
   countrydata order by country ASC");
while($row=mysql_fetch_row($result)){
    $id=$row[0];
    $name=$row[1];
    echo $name."&nbsp;".$id."<br>";
}

Upvotes: 0

juergen d
juergen d

Reputation: 204854

Either

select * from your_table
order by country <> 'France',
         country

or

select * from your_table
order by case when country = 'France'
              then 1
              else 2
         end,
         country

Upvotes: 3

Related Questions