Order items in MySQL by a fixed list? - mysql

I am going to have a fixed list of items to order by that I won't know until I run the query since there is a randomization step.
I would like to have something like the following:
Assume that is_launch_set will return 1, 3, 7, 11 but have been randomized to below:
SELECT * FROM items WHERE is_launch_set=1 ORDER BY id values (3,11,7,1);
Any ideas on how to achieve this? I was thinking maybe a find_in_set but not really sure.

You can do that by using either:
ORDER BY FIND_IN_SET(id, '3,11,7,1')
or
ORDER BY FIELD(id, 3, 11, 7, 1)
or
ORDER BY CASE id WHEN 3 THEN 0
WHEN 11 THEN 1
WHEN 7 THEN 2
WHEN 1 THEN 3
ELSE 4
END

In mys sql 8.0 it order only by the first column - 3 in this case:
ORDER BY FIND_IN_SET(id, '3,11,7,1')
With this example it worked, but upside. you'll see the 3 last, then 11, then 7 etc.
ORDER BY FIELD(id, 3, 11, 7, 1)
If you chose this example, you'll see the 1 first, then 7, then 11 etc.
ORDER BY FIELD(id, 3, 11, 7, 1) DESC

Related

How to use "ORDER BY FIELD" to sort the result of a SQL

If there is a data shown as below;
id cnt_stamp
1 999
2 3
3 9
4 3
5 1000
6 30
If an input is (4, 1, 2, 3) in this order, I would like to get only (3, 999, 3, 9).
To achieve this, I created a SQL
SELECT `cnt_stamp`
FROM `stm_events`
ORDER BY FIELD(`id`, 4, 1, 2, 3);
But it returns (1000, 30, 3, 999, 3, 9) instead. How should I fix my SQL to achieve my goal? Thank you for taking your time.
FIELD will assign NULL to any non matching id, and nulls sort first by default in MySQL. If you don't want to see non matching items at all you may just add a WHERE clause:
SELECT cnt_stamp
FROM stm_events
WHERE id IN (1, 2, 3, 4)
ORDER BY FIELD(id, 4, 1, 2, 3);
If you want to see all your data, with non matching id values at the end, then reverse the order of the field list and sort descending:
SELECT cnt_stamp
FROM stm_events
ORDER BY FIELD(id, 3, 2, 1, 4) DESC;
Demo
Use COALESCE function :
SELECT `cnt_stamp`
FROM `stm_events`
ORDER BY COALESCE(`id`,FIELD(`id`,4,1,2,3));
SQL Fiddle Demo

SQL: Sort list for children shoe sizes

As of right now I have a list that puts out shoe sizes.
This is what the list looks like for example.
1, 1.5, 2, 2.5, 10, 10.5, 11, 12, 13.5
And I need it to look like this
10, 10.5, 11, 12, 13.5, 1, 1.5, 2, 2.5
Thank you I know you can do this with an ORDER BY but I have failed. Thank you
Here is what example of what I tried.
ORDER BY priority=0, priority asc
If you can project a computation of the ordering, you can then use this in the ORDER BY, e.g. in your example it seems that by adjusting sizes 10+ to below the <10 sizes, and then still ordering ascending:
SELECT * FROM Shoes
ORDER BY
CASE WHEN Size >= 10
THEN Size - 20
ELSE Size
END ASC;
SqlFiddle here
You should add another column to your sizes table 'size_type'. The new column should have two possible values: adult and child.
You would do your first sort on the 'size_type' column and your second sort on the size column.

PHP order by clause with array as order by statement

I got a array with a query
This array is ordered by criteria.
Now I want to make a new query
Sample
$array = (987, 2661, 12, 789, 54);
And I want in this order by array selecting articles
select * from article a.number WHERE (a.number IN ($array))
How can I realize that this result is ordered by $array ids?
Thx you 4 answer guys :)
edit :
Article Table:
id, name etc..
Property Table:
id, article_id, name, value
1, 10, journey_days, 2
2, 30, journey_days, 1
3, 40, journey_days, 5
1, 10, stars, 2
2, 10, stars, 4
3, 10, stars, 0
4, 10, stars, 1
I join both tables, but as you can see the property have more then one value per column for one article.
I need to join the Property table to the article table and get all Values there are related from property table to the article, if I make a where clause I just get stars or journey_days.
How can I realise this? to select all property.name values with a where or on clause?
Hope you guys understand my question
Use implode:
$s = implode(",", $array);
$q = "select * from article a.number WHERE (a.number IN ($s))";
You could construct a CASE expression and order by that, but it's probably better to do this in PHP, outside of the database.
... ORDER BY CASE a.number
WHEN 987 THEN 1
WHEN 2661 THEN 2
WHEN 12 THEN 3
WHEN 789 THEN 4
WHEN 54 THEN 5
END;
Or you could use the FIND_IN_SET function:
.... ORDER BY FIND_IN_SET(a.number, "987,2661,12,789,54");

SQL using count value as offset

This is a follow on from another question i made
I have this query that counts all the records up to a certain point i choose whcih works fine
SELECT count(*)FROM news WHERE id < 18
this query gives me a count of 7
I am now having problems with the offset which would be the result of the above query
I tried using this query
SELECT * FROM `news` ORDER BY id DESC LIMIT 7,1
but i get id number 13 instead of 18
The ids i should have is 2, 7, 10, 11, 12, 13, 16, 18, 19, 20, 21, 22, 23
I have tried using order by id desc in the count query which does give a different result but still wrong id displayed
I dont see a problem here: You order the result by id DESC which means your result is ordered by other way around and 8th value(0..7) is 13.
Try sorting it by ASC then it will give you 18

SQL - select first in value set by priority?

I'm fairly inexperienced in SQL and this seems like it must be an easy task, but I'm not sure how to go about it.
Basically I want to select a single row from table A where field "someField" is in a pre-determined set "someSet", but I want it to look for each value in the set individually. For example, let's say "someSet" contains 5, 6, 9, 3. I would use a query similar to this:
SELECT * FROM A WHERE someField IN (5, 6, 9, 3) LIMIT 1
However, I want it to look for 5 first, then 6, then 9, then finally 3 if no rows have been found yet. Written as separate queries it'd look like this:
SELECT * FROM A WHERE someField = 5 LIMIT 1
(if no results returned)
SELECT * FROM A WHERE someField = 6 LIMIT 1
(if no results returned)
SELECT * FROM A WHERE someField = 9 LIMIT 1
(if no results returned)
SELECT * FROM A WHERE someField = 3 LIMIT 1
Obviously using 4 queries (theoretically infinite queries) isn't very elegant, is there a way to make this into a single query?
You can do
SELECT * FROM A WHERE someField IN (5, 6, 9, 3)
ORDER BY FIELD( someField, 5, 6, 9, 3)
LIMIT 1