Sort MySQL rows by column, but not alphabetically - mysql

Sorry if the title is a bit ambiguous and reminiscent of other semi-related questions), the issue is in fact quite simple.
I have a VARCHAR column which can have 1-character values such as M,G,D and S. If I sort the results alphabetically, in this example it will show them in the order: D-G-M-S. However, I need to display the rows in the following order:
G-D-M-S
Is there a way to accomplish this within the query? I know I can custom-sort the results in PHP, but I'd rather do it within the query if possible. For this example, I just need to switch the order of "G" and "D" in the results, and the solution to that simplistic problem will suffice for any answers.

You can write your custom case statement:
Select *
from your_table
order by
case your_column
when 'G' then 1
when 'D' then 2
when 'M' then 3
when 'S' then 4
end
Also, another solution, is to change collation at physical level:
Change default Sorting by Adding a Simple Collation to an 8-Bit Character Set

What I would do is define a temp or permanent table with 2 columns :
letter | ordernum
-------------------
G | 1
D | 2
M | 3
S | 4
Then you join your exiting table to that new one on the field "letter", and use the new table "ordernum" field to do the sort...

SELECT
if(columnname='G',1,
if(columnname='D',2,
if(columnname='M',3,
if(columnname='S',4,0
)))) SortOrder
FROM tablename
ORDER BY
SortOrder ASC

Select col from table
order by case when col = 'G' then 1
when col = 'D' then 2
when col = 'M' then 3
case when col = 'S' then 4 else 5 end ;

Related

SQL query statement Self Join?

new to SQL.
I have the following set of data
A X Y Z
1 Wind 1 1
2 Wind 2 1
3 Hail 1 1
4 Flood 1 1
4 Rain 1 1
4 Fire 1 1
I would like to select all distinct 'A' fields where for all rows that contain A have flood and rain.
So in this example, the query would return only the number 4 since for the set of all rows that contain A = 4 we have Flood and Rain.
I need the values of A where for a given value 'a' in A, there exists rows with 'a' that must contain all of the following fields provided (in the example Flood and Rain).
Please let me know if you need further clarification.
I need the values of A where for a given value 'a' in A, there exists rows with 'a' that must contain all of the following fields provided (in the example Flood and Rain).
You can use aggregation, and filter with a having clause:
select a
from mytable t
where x in ('Flood', 'Rain') -- either one or the other
having count(*) = 2 -- both match
If tuples (a, x) tuples are not unique, then you want having count(distinct x) = 2 instead.
You Shooud use count(distinct X) group by A and having
count(distinct...) avoid situation where you have two time the same value for X
select A
from my_table
WHERE x in ('Flood', 'Rain')
group A
having count(distinct X) = 2

How to define a custom ORDER BY in MySQL query

I need output in following order(firstly, group by last 3 letters and then arrange in order based on the first 3 digits)
ColumnA
001_eng
004_eng
002_chn
003_usa
But order by ColumnA gives me
ColumnA
001_eng
002_chn
003_usa
004_eng
This is just sample data. I have hundreds of entries in this format and the values keep changing everyday. So, specifying all the entries inside the field is not a feasible option.
I'm not sure of how to use FIELD() in my case.
You can use FIELD:
select *
from tablename
order by
FIELD(ColumnA, '001_eng', '004_eng', '002_chn', '003_usa')
(please be careful if ColumnA is not in the list the field function will return 0 and the rows will be put on top)
or you can use CASE WHEN:
select *
from tablename
order by
case
when ColumnA='001_eng' then 1
when ColumnA='004_eng' then 2
when ColumnA='002_chn' then 3
when ColumnA='003_usa' then 4
else 5
end
or you can use a different languages table where you specify the order:
id | name | sortorder
1 | 001_eng | 1
2 | 002_chn | 3
3 | 003_usa | 4
4 | 004_eng | 2
then you can use a join
select t.*
from
tablename t inner join languages l
on t.lang_id = l.id
order by
l.sortorder
(with proper indexes this would be the better solution with optimal performances)
You can use SUBSTRING_INDEX in case all ColumnA values are formatted like in the sample data:
SELECT *
FROM mytable
ORDER BY FIELD(SUBSTRING_INDEX(ColumnA, '_', -1), 'eng', 'chn', 'usa'),
SUBSTRING_INDEX(ColumnA, '_', 1)
Demo here
you can use substring() and get order by
SELECT *
FROM table_name
ORDER BY SUBSTRING(ColumnA, -7, 3);

count comma-separated values from a column - sql

I want count the length of a comma separated column
I have use these
(LENGTH(Col2) - LENGTH(REPLACE(Col2,",","")) + 1)
in my select query.
Demo:
id | mycolumn
1 2,5,8,60
2 4,5,1
3 5,Null,Null
query result for first two row is coming correctly.for 1 = 4 ,2 = 3 but for 3rd row it is calculating null value also.
Here is what I believe the actual state of your data is:
id | mycolumn
1 2,5,8,60
2 4,5,1
3 NULL
In other words, the entire value for mycolumn in your third record is NULL, likely from doing an operation involving a NULL value. If you actually had the text NULL your current query should still work.
The way to get around this would be to use COALESCE(val, "") when handling the NULL values in your strings.
Crude way of doing it is to replace the occurances of ',Null' with nothing first:-
SELECT a.id, (LENGTH(REPLACE(mycolumn, ',Null', '')) - LENGTH(REPLACE(REPLACE(mycolumn, ',Null', ''),",","")) + 1)
FROM some_table a
If the values refer to the id of rows in another table then you can join against that table using FIND_IN_SET and then count the matches (assuming that the string 'Null' is not an id on that other table)
SELECT a.id, COUNT(b.id)
FROM some_table a
INNER JOIN id_list_table b
ON FIND_IN_SET(b.id, a.mycolumn)
GROUP BY a.id

MySQL query (condition)

I want to achieve one thing though I'm not sure if it's possible.
So let's say I have a table with few columns, but only two of them are of interest to me right now. Example of table:
Column 1 | Column 2
blabla | blablahhhhh
wor154 | blablahhhhh
word123 | word12435564
something | some4565
What I want to achieve, is to select all fields where first 5 or more symbols of value of Column 2 don't match with first 5 or more symbols of value of Column 1. So I don't want to select rows where 5 or more symbols of value of Column 1 match 5 or more symbols of value of Column 2. In example, query should return only 2nd and 4th rows
So, is it possible and if it's, how it can be achieved. Thank you.
I'd go with a SUBSTRING():
SELECT col1 FROM table WHERE SUBSTRING(col1, 1, 5) <> SUBSTRING(col2, 1, 5);
You can use something similar to this:
select *
from table1
where substring(column1, 1, 5) != substring(column2, 1, 5)
See SQL Fiddle with Demo

ORDER BY discrete values of text column on [My]SQL[ite]

I would like to sort the rows in a table based on an arbitrary ordering of a limited set of values of a text (or enum) column. For instance, if column "Classes" can have the values A, B, C, D, I would like to be able to sort rows by A > C > B > D or by some other combination, and not just alphabetically or by the native order of the enum. Is that even possible?
Ideally, I would like a solution that works on SQLite, but a MySQL one would also be useful. Many thanks!
ORDER BY field(column, 'A', 'C', 'B', 'D')
FIELD function in MySQL docs
If mySQL and sqlite sllow the use of CASE in ORDER BY clauses (MSSQL does and I see no reason why other systems won't), you could use something like:
ORDER BY CASE
WHEN classes ='A' THEN 1
WHEN classes ='C' THEN 2
WHEN classes ='B' THEN 3
WHEN classes ='D' THEN 4
ELSE 5
END CASE
If the column can contain more than one of the options in any particular order you could also do:
ORDER BY CASE
WHEN classes LIKE '%A%' THEN 1
WHEN classes LIKE '%C%' THEN 2
WHEN classes LIKE '%B%' THEN 3
WHEN classes LIKE '%D%' THEN 4
ELSE 5
END CASE
though overloading a field like that is not a normal form and is generally not recommended.
Yet another variant - add a new (temporary?) table such as
class | rank
A | 1
B | 2
C | 3
D | 4
Then you can join the table by the class column and order by the rank.