Best way to perform mySQL queries and arrange the results - mysql

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

Related

MySQL ORDER BY in Query

I have a SQL Query that uses a standard ORDER BY syntax for a single column:
ORDER BY ScheduleDateCurrent DESC
This works great EXCEPT when that value is NULL for some records.
I tried specifying a backup column to be used instead:
ORDER BY ScheduleDateExact DESC, ScheduleDateCurrent DESC
I also tried:
ORDER BY ScheduleDateExact IS NOT NULL DESC, ScheduleDateCurrent DESC
but the results did not turn out as I wanted.
Results returned like this:
ScheduleDateExact: 8/3/2018
ScheduleDateExact: 8/1/2019
ScheduleDateCurrent: 8/3/2018
I want them returned like this:
ScheduleDateExact: 8/3/2018
ScheduleDateCurrent: 8/3/2018
ScheduleDateExact: 8/1/2019
I want the 8/3/2018 records to be returned at the top of the results regardless of column. How do I do this?
You can use expressions in order by clauses; something like this should work for you:
ORDER BY COALESCE(ScheduleDateExact, ScheduleDateCurrent)
which is the equivalent to
ORDER BY IFNULL(ScheduleDateExact, ScheduleDateCurrent)
or more portably
ORDER BY CASE WHEN ScheduleDateExact IS NULL THEN ScheduleDateCurrent ELSE ScheduleDateExact END

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

How to order by multiple fields in 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

Mysql : Sort by id DESC but also sort by another column and ignore value 0

Im trying to figure out if this is possible.
I want to sort by the id in DESC order, but there are certain records in the table that need to be pushed to the top.
I tried some ways, here is one ex. Maybe someone can help me out here?
SELECT *
FROM `table`
ORDER BY CASE WHEN index >0
THEN index
END , id DESC
LIMIT 0 , 30
But I cant seem to get the right output.
Almost! Try this:
ORDER BY CASE WHEN index > 0
THEN 0
ELSE 1
END, id DESC

MySQL ORDER BY, use column mod_time, but if mod_time is 0, use column create_time

I want to order results based on two columns, mod_time and create_time.
I want the most recent to be present. At the moment I have
ORDER BY pr.mod_time DESC, pr.create_time DESC
This breaks if the item has not modified, in which case mod_time is 0 and only the create_time is set. This effectively puts anything with a mod_time of 0 to the end of the ordering even if the create_time is greater than any other mod_time.
I hope that makes sense.
Is there a way I can use ORDER BY to do this?
Thanks, Jake
You could use this:
ORDER BY CASE WHEN pr.mod_time = 0 THEN pr.create_time
ELSE pr.mod_time
END DESC, pr.create_time DESC
Or perhaps this simpler version is want you want, assuming an item will never be modified before it is created:
ORDER BY GREATEST(pr.mod_time, pr.create_time) DESC, pr.create_time DESC
Note that these queries won't be able to use an index, if any.
I'm not sure if this is what you mean, but I'll offer it in case:
Just switch your ORDER BY around:
ORDER BY pr.create_time DESC, pr.mod_time DESC
This will cause it to sort by create_time first.
A side note: You could set mod_time at create time, such that a created item was 'modified' (created) at the same time as create_time. This probably depends on what else is going on in your system though.
Add IFNULL(pr.create_time, pr.mod_time) as one of the selected columns.
Specify the position of the IFNULL in the ORDER BY.