Add an overall order with case order - mysql

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

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

SQL select with custom ORDER BY data

I have data TSA, TSB, TSC, Total.
How to display this alphabetically with Total always the last one.
Currently I have this and of course it doesn't work.
select * from table where main_id =x group by col-name asc
Certainly I can't use desc because I have another record ABC, BCA, CDA, Total.
So how to add "except if col-name is Total"? or perhaps there is another way?
Kind of hard to be sure with your post (not a lot of detail in there). But you could probably use a case statement to evaluate your column, and sort on that. Something like
case when <your column> = 'Total' then 'ZZZ' else <your column> end as SortKey
Then you can just order by that new column.
User order by field:
select * from table
where main_id =x
order by FIELD( `col-name`, 'Total' ), t;
See:
SQL Fiddle Example
Refer to:
MySQL: FIELD(str,str1,str2,str3,...)
Return the index (position) of the first argument in the subsequent
arguments.

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 query with WHERE clause, but drop a condition if no results?

I'm currently doing this via two separate queries from PHP, but would love to optimize and somehow in a single query.
First query..
SELECT `referrer`
FROM `tbl_traffic_log`
WHERE `domain` = 'mysite.com'
AND `referrer` != '$referringDomain'
AND CASE WHEN `clicks_in_unique`=0 THEN 2 ELSE `clicks_out_unique`/`clicks_in_unique` END < 1.4
ORDER BY RAND()
LIMIT 1
..and if mysql_num_rows shows no results, I do a second query to try again and check if there are any results minus the referrer != 'partner1.com' part.
The code is basically trying to find a random trade partner who ISN'T the partner who sent that click, but if there are no matches, as a last resort it's ok to send back, provided it matches the other criteria.
I'm pretty sure there is a way to do this, but just can't find a way after searching (probably because I'm not understanding the problem enough to type in the right thing).
Any other critique of the query is welcome as well.
Thank you :)
I think you can do this like this:
SELECT `referrer`
FROM `tbl_traffic_log`
WHERE `domain` = 'mysite.com'
AND CASE WHEN `clicks_in_unique`=0 THEN 2 ELSE `clicks_out_unique`/`clicks_in_unique` END < 1.4
ORDER BY `referrer` != '$referringDomain' desc, RAND()
LIMIT 1
The idea is to put the condition in the order by. The condition (in MySQL) evaluates to either 0 or 1, so we want where the condition is true first (hence the desc). It then chooses a random row. If there are no rows where the condition is true, then it chooses a random row.

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.