SQL select with custom ORDER BY data - mysql

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.

Related

Mysql: Is it possible to use a subquery inside a from clause in order to pick the table name from another table

I was wondering if there's any way to add a subquery with a switch case to the form clause of my select query in order to select a table based on a condition.
For example:
select a.*
from (select (case when (table2.column = 'something')
then (table2.tablename1)
else (table2.tablename2)) as tablename
from table2
where table2.column2 = 'blabla'
limit 1
) a
I tried to write that in many variation & so far non of them worked.
On the most successful tryouts (when I got no mysql errors) it returned the name of the table as the result itself (for example: the value that's in table2.tablename2). I understand why it did that (because I selected everything from a select results...) but how can I use the tablename from the results in order to set the table on the main query?
Hope that make sense...
Any idea?

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

How to order your result in a specific way in SQL Server

I have a simple query, but I would like to see the results in a specific way. I would like to see 'N/A' at the top of the result, without having to result to "Case When Then"
Select *
From Ordertype
Results:
Car21
Car34
Bus42
N/A
Thanks,
There are no 'overrides' for ORDER BY, if you want the specific order you're asking for, you'll have to use the CASE:
SELECT type
FROM OrderType
ORDER BY
CASE
WHEN type = 'N/A' THEN 1
ELSE 2
END
,type
If you want an arbitrary order that is not tied directly to the structured of a column (alphabetical/numerical) but rather to it's importance which only you know in your head it can be useful to add a Rank column to your table.
Column1 Rank
Car21
Car34 2
Bus42 1
N/A 99
then you can do
select Column1
from Table
order by rank desc, column1
This will put highly ranked items first then low ranked items, then when rows don't have a rank it will sort them alphabetically by column1
You can try this:
SELECT * FROM ordertype ORDER BY ID DESC
to see the newest ones 1st

ORDER BY FIELD() and duplicate data

Lets say I have the following query.
SELECT stringdata FROM table ORDER BY FIELDS( stringdata, 'tg','nk','mg','pl') asc;
For some reason I'm getting the results at the very bottom. How can I get the query to put the results starting from 'tg' at row 1 rather than the last row in the results?
Not only that but there's more than one 'tg' in the data, I'd like it to sort it in this expected output:
stringdata
__________
'tg'
'tg'
'tg'
'nk'
'nk'
'mg'
'mg'
'mg'
'pl'
So far using ORDER BY Fields() is only sorting one instance of the data rather than all.
Using desc instead of asc in the query works as expected. I get 'pl' on the first row, then 'mg', 'nk', etc.
Normally the FIELD clause in ORDER BY works something like
SELECT * FROM table ORDER BY FIELD(field, high_priority, second_high,
,....., low_priority);
So in your query, the sorting took place as you mentioned and when you gave the ASC it printed from the lowest_priority. So, for your case, if you want tg at the top, you can either reorder the priority in the FIELDS or as you have already tried use desc
If you want first rows with 'tg', then rows with .... then with 'pl' and then all the rest sorted (ASC or DESC) use this:
SELECT stringdata
FROM table
ORDER BY FIELD( stringdata, 'pl','mg', 'nk', 'tg') DESC
, stringdata ASC --- or DESC

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