MySQL - ORDER BY multiple words, then alphabetically? - mysql

How can I change this part of the query to have multiple words ordered first in sequence, then the rest of the results alphabetically?
ORDER BY CASE WHEN name LIKE '%Professional%' THEN 0 ELSE 1 END asc, name asc
So, it needs to be:
'%Professional%' 1
'%Leader%' 2
'%Advocate%' 3
'%Clinician%' 4
'%Educator%' 5
'%Scholar%' 6
Then all other results alphabetically.

You can just expand your CASE expression with each of the desired words:
ORDER BY
CASE WHEN name LIKE '%Professional%' THEN 1
WHEN name LIKE '%Leader%' THEN 2
WHEN name LIKE '%Advocate%' THEN 3
WHEN name LIKE '%Clinician%' THEN 4
WHEN name LIKE '%Educator%' THEN 5
WHEN name LIKE '%Scholar%' THEN 6
ELSE 7
END,
name

Related

Define a custom ORDER BY order in mySQL using the LIKE word

How can I make a custom order where I sort the rows by NAME where the first rows' name has Bob in it followed by rows with name of Alex in it?
To explain what exactly I mean: I have made the following query to sort result if NAME = 'Bob' and if NAME = 'Alex':
SELECT * FROM table
ORDER BY CASE `NAME`
WHEN 'Bob' THEN 1
WHEN 'Alex' THEN 2
ELSE 3
END
But this only works when the NAME is exactly equal to Bob or Alex. I want to modify it to sort if the NAME has Bob or Alex in it, essentially if NAME LIKE '%Bob%' and NAME LIKE '%Alex%'. I tried something like the following but it does not work.
ORDER BY CASE `NAME`
WHEN LIKE '%Bob%' THEN 1
WHEN LIKE '%Alex%' THEN 2
ELSE 3
END
What is the correct syntax for this?
Use the other form of CASE where you specify a condition in WHEN rather than a value.
ORDER BY CASE
WHEN NAME LIKE '%Bob%' THEN 1
WHEN NAME LIKE '%Alex%' THEN 2
ELSE 3
END

Return rows matching one condition and if there aren't any then another in MYSQL

I have the following table as an example:
numbers type
--------------
1 1
5 2
6 1
8 2
9 3
14 2
3 1
From this table I would like to select the closest number that is less or equal to 5 AND of type 1 and if there is no such row matching, then (and only then) I would like to return the first closest number larger than 5 of type 2
I can solve this by running two queries:
SELECT number FROM numbers WHERE number <= 5 AND type = 1 ORDER BY number LIMIT 1
and if above query returns 0 results, I simply run the second query:
SELECT number FROM numbers WHERE number > 5 AND type = 2 ORDER BY number LIMIT 1
But is it possible, to achieve the same result by only using one query?
I was thinking something like
SELECT number FROM numbers WHERE (number <= 5 AND type = 1) OR (number > 5 AND type = 2) ORDER BY number LIMIT 1
But that would only work, if mysql first checks the first conditional in the parentheses against all rows and if it finds a match, it returns it, and if not, then it checks all rows against the second parenthesed conditional. It will not work, if it checks each row against both parentheses and only then moves to the next row, which is how I suspect it works.
This query will do what you want. It selects all numbers that match your two query constraints, and orders the results first by type (so that if there is a result for type 1 it will appear first) and then by either -number or number dependent on type (so that numbers <= 5 sort in descending order but numbers > 5 sort in ascending order):
SELECT number
FROM numbers
WHERE ( number <= 5 AND type = 1 )
OR ( number > 5 AND type = 2 )
ORDER BY type, CASE WHEN type = 1 THEN -number ELSE number END
LIMIT 1
Output:
3
Demo on dbfiddle
Combine the two, and you always prefer type 1 over type 2, hence the ORDER BY and LIMIT. The ABS means whichever is first by type, is the closes to the number 5.
SELECT number, type
FROM numbers
WHERE (number <=5 AND type=1) OR
(number > 5 AND type=2)
ORDER BY type ASC, ABS(number-5) ASC
LIMIT 1

how to move up row to find the last or first occurrence of the same number in mysql

I have a table name current_record in mysql database.
id num
1 0
2 1
3 1
4 1
5 0
my question is : how find the 1st id of num = 1 from last means down to up or 3 times up
the output should return like this
id = 2 num = 1
please write a sql query.
You can use MIN() combined with GROUP BY to achieve this. Something like this:
SELECT MIN(id) AS id, num FROM current_record GROUP BY num
Optionally you can add a WHERE clause if you specifically want just one value instead of one for each num:
WHERE num == 1

order by case in sql 2005 server

One of the column in my table like this:
Symbol
586fast
urgent
243late
296fast
122late
155fast
I need urgent in first then records with fast order asc then records with late order asc like this:
urgent
586fast
296fast
155fast
243late
122late
I am getting urgent in first row,records with fast and late but they are not in asc order
ORDER BY CASE
when Symbol like '%FUT' then 1
when Symbol like '%CE' then 2
when Symbol like '%PE' then 3
else 4 end
After ordering using case statement provide further ordering by Symbol column itself
ORDER BY CASE
when Symbol like '%FUT' then 1
when Symbol like '%CE' then 2
when Symbol like '%PE' then 3
else 4 end, Symbol asc

sort particular rows using mysql order case

I have a table with incremented id from 1,2,3...and so on. What i want is just to sort the data in descending order on the basis of field 'id' except first two rows. I tried using below query:-
SELECT * FROM categories ORDER BY CASE WHEN id<3 THEN 0 ELSE id END DESC
It give me the result like
id name
5 Meal
4 Apparel
3 Electronics
1 Sports
2 Lifestyle
But output should come like
id name
1 Sports
2 Lifestyle
5 Meal
4 Apparel
3 Electronics
Is there any way to achieve this by using such query?
Try this:
SELECT *
FROM categories
ORDER BY CASE WHEN id < 3 THEN 10000 - id ELSE id END DESC;
-- ----------------------------^
-- use a very large number
Edit:
A better solution which does not require hard coding 10000:
SELECT *
FROM categories
ORDER BY CASE WHEN id < 3 THEN id ELSE 3 END, id DESC
-- ---------------------^ ^
-- ------------------------------------|
-- these numbers must be same
Change it to:
CASE WHEN id<3 THEN id ELSE ~id END
And instead of case, use IF:
IF(id<3, id, ~id)
That's because you're ordering by descending ids, so the order is 5-4-3-0-0. You can try:
SELECT * FROM categories ORDER BY CASE WHEN id<3 THEN 9999 ELSE id END DESC
But that is not a perfect solution