Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select query using IN() and without any sorting

My query

select * from product where productId in(25,36,40,1,50);

Result shows as follows

`productId   ProductName  Qty Price`
-------------------------------------
`1          | namesome  | 5 | 25.00`
`25         | namesome  | 5 | 35.00`
`36         | namesome  | 5 | 35.00`
`40         | namesome  | 5 | 35.00`
`50         | namesome  | 5 | 35.00`

I did not use any order by clause, But its automatically applied order by productId,
I need result with out any sort, as follows

`productId   ProductName  Qty Price`
-------------------------------------
`25        | namesome  | 5 | 25.00`
`36        | namesome  | 5 | 35.00`
`40        | namesome  | 5 | 35.00`
`1         | namesome  | 5 | 35.00`
`50        | namesome  | 5 | 35.00`

How can I achieve this?
Database Engine: MyIsam, Collation: utf8_general_ci, PrimaryKey on productId

like image 337
user1844933 Avatar asked Dec 01 '12 08:12

user1844933


People also ask

How does SQL sort without ORDER BY?

In SQL Server if you don't specify an order then you may get either an index scan or an allocation ordered scan for example. Plus also you might encounter the "advanced scanning" / merry-go-round scanning feature.

Can I use SELECT statement without from?

Only some database systems support a SELECT statement which has no FROM clause (Microsoft SQL Server and MySQL do). If your database system requires a FROM clause, then all you need to do is select the strings from any convenient table where it is easy to target a specific row.

Can we use SELECT statement in ORDER BY clause?

SQL queries initiated by using a SELECT statement support the ORDER BY clause. The result of the SELECT statement is sorted in an ascending or descending order.

Which query is used for sorting data?

The ORDER BY statement in SQL is used to sort the fetched data in either ascending or descending according to one or more columns. By default ORDER BY sorts the data in ascending order. We can use the keyword DESC to sort the data in descending order and the keyword ASC to sort in ascending order.


1 Answers

select * 
from product 
where productId in(25,36,40,1,50) 
order by find_in_set(productId, '25,36,40,1,50');

See this SQLFiddle

like image 144
Hanky Panky Avatar answered Sep 21 '22 00:09

Hanky Panky