Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql custom sort

Tags:

sorting

mysql

I have a query like this: SELECT * FROM table WHERE id IN (2,4,1,5,3);

However, when I print it out, it's automatically sorted 1,2,3,4,5. How can we maintain the order (2,4,1,5,3) without changing the database structure?

Thanks!

like image 656
Patrick Avatar asked Feb 01 '10 08:02

Patrick


People also ask

What is custom sorting in SQL?

By default SQL ORDER BY sort, the column in ascending order but when the descending order is needed ORDER BY DESC can be used. In case when we need a custom sort then we need to use a CASE statement where we have to mention the priorities to get the column sorted.

How do I sort in MySQL?

The ORDER BY keyword is used to sort the result-set in ascending or descending order. The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.

How do I change the order of rows in MySQL?

An "ALTER TABLE ORDER BY" statement exist in the syntaxes accepted by MySQL. According to the documentation, this syntax: - only accept *one* column, as in "ALTER TABLE t ORDER BY col;" - is used to reorder physically the rows in a table, for optimizations.

How do I sort multiple columns in MySQL?

Summary. Use the ORDER BY clause to sort the result set by one or more columns. Use the ASC option to sort the result set in ascending order and the DESC option to sort the result set in descending order. The ORDER BY clause is evaluated after the FROM and SELECT clauses.


4 Answers

SELECT * FROM table WHERE id IN (2,4,1,5,3) ORDER BY FIELD(id,2,4,1,5,3);

Source: http://imthi.com/blog/programming/mysql-order-by-field-custom-field-sorting.php

like image 189
Lucas Rossini Avatar answered Oct 18 '22 18:10

Lucas Rossini


i ask this :

mysql order by issue

the answers that i get and all the credit belong to them is :

You can use a CASE operator to specify the order:

SELECT * FROM table
WHERE id IN (3,6,1,8,9)
ORDER BY CASE id WHEN 3 THEN 1
                 WHEN 6 THEN 2
                 WHEN 1 THEN 3
                 WHEN 8 THEN 4
                 WHEN 9 THEN 5
         END

in php u can do it like :

<?php

$my_array =  array (3,6,1,8,9) ;

$sql = 'SELECT * FROM table  WHERE id IN (3,6,1,8,9)';

$sql .= "\nORDER BY CASE id\n";
foreach($my_array as $k => $v){
    $sql .= 'WHEN ' . $v . ' THEN ' . $k . "\n";
}
$sql .= 'END ';

echo $sql;

?>
like image 41
Haim Evgi Avatar answered Oct 18 '22 18:10

Haim Evgi


(I would have written this as a comment on Michel Tobon's answer, but don't have the reputation, sorry :-)

"And it worked... why? Beats me, but it just did; try it as well."

The reason that works is because your expression "code!='USA'" is producing a boolean result, which in SQL is represented as a 1 or 0. So, the expression "code='USA' produces a 1 for every record that matches that criterion, and a 0 for every record that does not. Because 1 is later than 0 in an ascending sort (the default), matching records will sort later than unmatching ones. Thus negating that expression producing the opposite effect.

Another (possibly clearer) way of producing the same result would be as follows:

SELECT * FROM countries ORDER BY code='USA' DESC, code='CAN' DESC, name ASC

Of course in answering the OP's question, I like the FIELD() option best - quite clean :-)

like image 40
opensourcejunkie Avatar answered Oct 18 '22 20:10

opensourcejunkie


The Ordering by field never worked for me. I had a list of countries and I needed United States and Canada to appear on top of the list, so my query was like this:

SELECT * FROM countries ORDER BY code='USA', code='CAN', name ASC

This never worked, but I realized that the ordering was different, it was showing Canada and the States at the end of the list, so I did this:

SELECT * FROM countries ORDER BY code!='USA', code!='CAN', name ASC

And it worked... why? Beats me, but it just did; try it as well.

like image 36
Michel Tobon Avatar answered Oct 18 '22 19:10

Michel Tobon