How to order by multiple fields in MySQL? - mysql

I'm looking to order data in a specific way where I have explicitly laid out which fields I want to appear first. Basically, I'm looking to return a MySQL query by doing something that I would imagine might look like this:
ORDER BY
FIELD(brand,'toyota','honda','ford'),
FIELD(type, 'SUV', 'Sedan', 'Coupe'),
FIELD(transmission, 'manual', 'automatic', 'cvt')
Simply said I'm looking for a way to order things specifically based on multiple fields. I've tried it like this but it doesn't seem to be working. Can this even be done or after I specify the order of one field do I have to only order other things by either ASC or DESC?
Thanks for your help!

Perhaps something like this?
SELECT brand, type, transmission
FROM tablename
ORDER BY
case brand
when 'toyota' then 1
when 'honda' then 2
when 'ford' then 3
end ASC,
case type
when 'SUV' then 1
when 'Sedan' then 2
when 'Coupe' then 3
end ASC,
case transmission
when 'manual' then 1
when 'automatic' then 2
when 'cvt' then 3
end ASC
http://www.devx.com/tips/Tip/17288

Related

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

Add an overall order with case order

I have a sql with a where statement and then an ordering statment.
My ordering statement is like this
'ORDER BY CASE
WHEN '.$col.' LIKE \''.$searchTerm.'\' THEN '.$c_0++.'
WHEN '.$col.' LIKE \''.$searchTerm.'%\' THEN '.$c_20++.'
WHEN '.$col.'LIKE \'%'.$searchTerm.'%\' THEN '.$c_40++.'
ELSE '.$c_60.' END,'.$col;
This gets done a couple of times depending on how many columns and then also splitting my search term up. This works pretty well but when there are couple of results matching the first order statement they display then on ID order, which is not what I want.
How do I add ORDER BY 'RELEASE_DATE' DESC to this.
I have tried adding it my sql in numorous places but just get sql santex errors coming back to me
You add another clause to the order by:
ORDER BY (CASE WHEN '.$col.' LIKE \''.$searchTerm.'\' THEN '.$c_0++.'
WHEN '.$col.' LIKE \''.$searchTerm.'%\' THEN '.$c_20++.'
WHEN '.$col.'LIKE \'%'.$searchTerm.'%\' THEN '.$c_40++;
ELSE '.$c_60.'
END),
(CASE WHEN '.$col.' LIKE \''.$searchTerm.'\' THEN RELEASE_DATE END) desc,
'.$col

mysql ordering exception

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

MySQL ORDER BY [custom SET field value]

I hope there's a simple solution for this:
I have a table where each row has it's own status (SET type field). Statuses can be:
offline
available
busy
distance
I'd like to order as follows
available
busy
distance
offline
I thought a simple
ORDER BY `status` ASC
will do the trick (alphabetical order) but it gives me the following:
offline
available
busy
distance
How can is sort out this in the most simple way?
Thanks in advance,
fabrik
SELECT * FROM table ORDER BY FIELD(status,'offline','available','busy','distance')
see Mysql order by specific ID values
Thought I'd add another way to order by custom field values,
ORDER BY FIND_IN_SET(status, 'available,busy,distance,offline')
(If the given strings contain a quote simply escape it)
You could also do something like this, if reordering the SET values in impractical:
... ORDER BY CASE `status`
WHEN 'available' THEN 1
WHEN 'busy' THEN 2
WHEN 'distance' THEN 3
WHEN 'offline' THEN 4
END
Simpler than the solutions above:
ORDER BY CONCAT(status)
Nevermind.
The trick is saving SET values at the right order.