mysql ordering exception - mysql

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

Related

MySql Query- Order By clause with field() method

I have the following MySql Query:
SELECT * FROM myTable ORDER BY FIELD(
priority,1,2,3,4,5,6,7,8,....,300,0),date_fixed ASC, sno DESC";
In fact, i need to display record ordered by priority ASC but also want to keep all the record having the priority = 0 at the end of the any other numerical value. The above query working fine but I think its not worth to write values starting from 1 to 300 and at the end i just put 0.
My Question is that any shortcut way to achieve the ResultSet as Orderby priority ASC and keeping the 0th priorities at the end?

Mysql forcibly order by exact value

Need help with mysql request.
I make a request and I want to order result by the field ID.
ID for exemple: 1,2,3,4,5,6,7,8,9
But I need that it would be ordered like this: 6,3,1,2,4,5,7,8,9
Is there any solution in mysql to forcibly put ID=6 in 1st place, ID=3 in 2nd...?
or I want the impossible)
Use CASE expression in ORDER BY clause.
Query
select * from `your_table_name`
order by case `id` when 6 then 1 when 3 then 2 else 3 end, `id`;
Find a demo here
Use:
ORDER BY FIELD(id, 6,3,1,2,4,5,7,8,9)
The FIELD function returns the position of the first value in the remaining list.

ORDER BY clause based on value - MySQL

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

Best way to perform mySQL queries and arrange the results

I need to be able to display the results of the below query in a specific order. For example: showing featured listings before the rest of the results.
WHERE IS `featured-listing` && WHERE IS NOT `featured-listing`
Could probably run 2 queries and a union right, bu is that the most effective solution? I know this can be done with one query I just cant remember how it/s done. Any and all help is appreciated.
SELECT `Assigned-Regions`,`Description`,`Category`,`Start-Date` FROM `adds` WHERE `Status` = "Active" ORDER BY `Start-Date` DESC
I would use a case statement for ORDER BY.
So something like
SELECT ... ORDER BY (CASE WHEN featured-listing THEN 1 ELSE 2) ASC, some-other-field ASC
Sounds like all you need is to add an ORDER BY clause to your query.
If featured-listing column is integer datatype and contains values of 1 or 0 (1=is featured listing, 0=not a featured listing), then you could simply add something as simple as:
ORDER BY `featured-listing` DESC, `Start-Date` DESC
Or, you could use an expression:
ORDER BY IF(`featured-listing`=1,1,0) DESC, `Start-Date` DESC
you can do conditional ordering.. not sure what featured-listing is without seeing some data but this is the logic for conditional ordering
SELECT `Assigned-Regions`,`Description`,`Category`,`Start-Date`
FROM `adds`
WHERE `Status` = "Active"
ORDER BY
CASE WHEN `featured-listing` THEN 1 ELSE 2 END ASC,
`Start-Date` DESC

How to order your result in a specific way in SQL Server

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