MySQL - order of ORDER BY - mysql

There is a MySQL query:
SELECT * FROM table ORDER BY param1, param2, param3
There is a table which holds the priorities of these params - param, priority (int).
I would like to execute the above query in such a way that the param which has the highest priority would be in first place in the ORDER BY statement, after this the second, and the third will be last one.
Is it possible in MySQL. If yes - how?

You dont order by just specific values in the column..you order it by the whole column, in your case column priority
SELECT * FROM table ORDER BY priority ASC
Priority 1 will be first ( you can use DESC if u want Priority 1 to be the last one)

In MySQL, you would use find_in_set() or case (the latter is the ANSI standard):
SELECT *
FROM table
ORDER BY find_in_set(priority, param1, param2, param3);

I do it like this
SELECT *
FROM table
ORDER BY FIELD(priority, param1, param2, param3);

Related

select ascending or descending according to a parameter

I have this query:
select -- fields--
from myTable
where -- conditions --
order by myField if('param'='1' ,'desc','')
if('param'='1' ,'desc',''): according to that param, I wanna sort ascending or descending.
Executing this query, I get this error:
check the manual that corresponds to your MySQL server version for the right syntax to use near 'if('param'='1' ,'desc','')'
Something like?
select -- fields--
from myTable
where -- conditions --
order by CASE WHEN #param=1 THEN myField ELSE -myField END;
If is generally used inside stored procedures, you have to use CASE/WHEN as show above. Also 'param' doesn't make sense , you probably wanted #param. lastly it's not possible to set ASC/DESC conditionally but the same objective can be achieved by taking the negative/positive of a fields value.
You can use a CASE/WHEN structure:
set #param = 1;
SELECT * FROM `myTable`
ORDER BY
CASE WHEN #param = 1 THEN myField END ASC,
CASE WHEN #param = 2 THEN myField END DESC;
remove the quotes from param and to this:
select -- fields--
from myTable
where -- conditions --
order by if(param ='1') desc
And if your param is a int or number for example, also remove the quotes from the value:
... if(param =1) desc
You can split the 2 situation using an if.. else.. and execute different blocks on different condition.
if(#param='1')
select -- fields--
from myTable
where -- conditions --
order by myField desc
else
select -- fields--
from myTable
where -- conditions --
order by myField

Concat 2 columns in a string, then get a count for each concatenation

I am trying to concatenate 2 columns, then count the number of rows i.e. the total number of times the merged column string exists, but I don't know if it is possible. e.g:
SELECT
CONCAT(column_1,':',column_2 ) as merged_columns,
COUNT(merged_columns)
FROM
table
GROUP BY 1
ORDER BY merged_columns DESC
Note: the colon I've inserted as a part of the string, so my result is something like 12:3. The 'count' then should tell me the number of rows that exist where column_1 =12 and column_2 = 3.
Obviously, it tells me 'merged_columns' isn't a column as it's just an alias for my CONCAT. But is this possible and if so, how?
Old question I know, but the following should work without a temp table (unless I am missing something):
SELECT
CONCAT(column_1,':',column_2 ) as merged_columns,
COUNT(CONCAT(column_1,':',column_2 ))
FROM
table
GROUP BY 1
ORDER BY merged_columns DESC
You can try creating a temp table from your concatenation select and then query that:
SELECT CONCAT(column_1,':',column_2 ) AS mergedColumns
INTO #temp
FROM table
SELECT COUNT(1) AS NumberOfRows,
mergedColumns
FROM #temp
GROUP BY mergedColumns
Hope this answer is what your are looking for.
Try this
SELECT
CONCAT(column_1,column_2 ) as merged_columns,
COUNT(*)
FROM
table
GROUP BY merged_columns
ORDER BY merged_columns DESC

Excluding first and last result from sql query

I'm fairly new to SQL so this may be fairly simple but I'm trying to write a script in SQL that will allow me to get a set of data but I don't want the first or last result in the query. I can find lots on how to remove the first result and how to remove the last result but not both.
This is my query so far:
SELECT * FROM itinerary Where ID = 'A1234' ORDER BY DateTime ASC
I want to remove the first and the last record of that select based on the DateTime.
This may not be the most performant way to do this, but you didn't give any schema info, and it looks like your ID column is not unique. It would be easier if you had a primary key to work with.
SELECT * FROM itinerary
WHERE ID = 'A1234'
AND DateTime <
(SELECT MAX(DateTime) FROM itinerary WHERE ID = 'A1234')
AND DateTime >
(SELECT MIN(DateTime) FROM itinerary WHERE ID = 'A1234')
ORDER BY DateTime ASC
This will basically select every record where the ID is A1234 and the DateTime doesn't equal the max or min datetime. Please note, if you have multiple records with the same value for DateTime and that also happens to be the min or max value, you might exclude more than just the first or last.
This might be good enough though. If not, you might need to write a stored procedure and not just straight ANSI SQL.
Try this ..
select * from
(select a.*,row_number() over (partition by DateTime order by DateTime desc) as rnm
from itinerary Where ID = 'A1234')x
where rm <> 1 and rm not in (
select max(rm) from
(
select row_number() over (partition by DateTime order by DateTime desc) as rnm
from itinerary Where ID = 'A1234'))
Select in reverse order & skip first and then select in the required order from the result, skipping first.
SELECT * FROM (SELECT *
FROM itinerary
Where ID = 'A1234'
ORDER BY DateTime DESC
LIMIT 1, 18446744073709551615) x ORDER BY DateTime ASC
LIMIT 1, 18446744073709551615
18446744073709551615 is max integer just in case you wanted to know why I picked that value

Select * from table where id = ? and order by priority

I have a database with those fields:
id (int)
name (text)
priority (int)
I want to show values from database where id = (e.g) 13 and sort them by priority.
E.G: The field with id 13 and priority 1 will be first from the other field with priority 2.
How can i do it?
SELECT id,name,priority from table_name WHERE id=13 ORDER BY priority ASC;
Your query:
SELECT `id`,`name`,`priority` FROM table_name WHERE `id`=13 ORDER BY `priority` ASC;
The ordering can be managed with the field function:
SELECT `id`,`name`,`priority` FROM table_name
order by FIELD(priority,"13","2")
Watch out for practical issues: YOu have to manually maintain this order list (or create some structure to maintain this).

Is possible to keep rows matching a condition always on top in MySQL

I have a MySQL table as following image shows:
Now I want to select the data by keeping the type=2 always on the top, like this:
How can I implement this using one SQL statement?
SELECT id,
type,
name,
register
FROM table
ORDER BY CASE type
WHEN 2 THEN 1000000
ELSE id
END CASE DESC
SELECT id,
TYPE,
name,
register
FROM table
ORDER BY field(TYPE,2) desc