I get a lot of answers when it comes to how MySQL sorts results but none seems to address my specific issue. Hope I didn't miss the answer somewhere else here on stackoverflow.
I have an SQL query that looks like this:
SELECT id, something FROM sometable WHERE
id=1 OR id=2 OR id=5 OR
id=6 OR id=100 OR id=1000
OR id=4
Now I need it to return the results in the specific order it has been selected. The results should look like this in this specific order:
1
2
5
6
100
1000
4
Will MYSQL display the returned records in this particular order the way I selected them? I cannot use ORDER BY because I need them the exact way I selected them in the first instance. A simple test confirms that it is returned in this way but from reading elsewhere I get the idea that you cannot trust the way results are being returned in specific order when ORDER BY is not used.
You can use FIELD() function. In your application code, where you will be creating this SQL dynamically, you can build the SQL string in the same order for FIELD(), as in your WHERE clause.
FIELD(str,str1,str2,str3,...)
Returns the index (position) of str in the str1, str2, str3, ... list.
Returns 0 if str is not found.
Now, the FIELD(id, 1,2,5,6,100,1000,4) function will return 0 if the id is not in (1,2,5,6,100,1000,4).
So, if we use ORDER BY FIELD(id, 1,2,5,6,100,1000,4) only, other non-matching rows will appear at the top. So, we can use If() function to return 1 for the other non-matching rows, and 0 for the matched rows.
Now, we can utilize one more level of ordering by FIELD(id, 1,2,5,6,100,1000,4). This will ensure that the matched id(s) appear first in the order as specified in the Field() function.
SELECT id, something FROM sometable WHERE
id=1 AND id=2 AND id=5 AND
id=6 AND id=100 AND id=1000
AND id=4
ORDER BY IF(FIELD(id, 1,2,5,6,100,1000,4) = 0, 1, 0) ASC,
FIELD(id, 1,2,5,6,100,1000,4) ASC
I have a table in a MySQL database.
One of the columns is entitled Failed and some rows have the value Yes in them.
How do I order my SELECT query so it shows the rows that have no value first and then shows the ones with Yes as the value at the end?
Just use ASC or DESC when ordering on the field(s) you wish to order by. In this case try:
ORDER BY Failed ASC
Use CASE in Order by
Order by case when col <> 'yes' then 2 else 1 end
I have a simple query, but I would like to see the results in a specific way. I would like to see 'N/A' at the top of the result, without having to result to "Case When Then"
Select *
From Ordertype
Results:
Car21
Car34
Bus42
N/A
Thanks,
There are no 'overrides' for ORDER BY, if you want the specific order you're asking for, you'll have to use the CASE:
SELECT type
FROM OrderType
ORDER BY
CASE
WHEN type = 'N/A' THEN 1
ELSE 2
END
,type
If you want an arbitrary order that is not tied directly to the structured of a column (alphabetical/numerical) but rather to it's importance which only you know in your head it can be useful to add a Rank column to your table.
Column1 Rank
Car21
Car34 2
Bus42 1
N/A 99
then you can do
select Column1
from Table
order by rank desc, column1
This will put highly ranked items first then low ranked items, then when rows don't have a rank it will sort them alphabetically by column1
You can try this:
SELECT * FROM ordertype ORDER BY ID DESC
to see the newest ones 1st
I have a MySQL database and a have a funny question.
I need to order the results of a query by a field which has entries of 1,2,3 or 4, ordered descending but with 4 at the end.
So I wish to have the results in the following order
3's
2's
1's
4's
Is this possible at all?
I know I can do order the result array in php but unfortunately I need to do this in an sql statement.
If the field is an int,
ORDER BY (fieldname = 4) DESC, fieldname DESC
should do the trick.
Here is another cool way
ORDER BY MOD(fieldname,4) DESC,fieldname
If the result is a CHAR(1) then
ORDER BY LOCATE(fieldname,'3214'),fieldname
add this to the order
ORDER BY
CASE field_name WHEN 4 THEN 1
ELSE 2
END
this will return the result of the query order using the value of the field
I have a MySQL table as following image shows:
Now I want to select the data by keeping the type=2 always on the top, like this:
How can I implement this using one SQL statement?
SELECT id,
type,
name,
register
FROM table
ORDER BY CASE type
WHEN 2 THEN 1000000
ELSE id
END CASE DESC
SELECT id,
TYPE,
name,
register
FROM table
ORDER BY field(TYPE,2) desc