Order by last 3 chars - mysql

I have a table like:
id name
--------
1 clark_009
2 clark_012
3 johny_002
4 johny_010
I need to get results in this order:
johny_002
clark_009
johny_010
clark_012
Do not ask me what I already tried, I have no idea how to do this.

This will do it, very simply selecting the right-most 3 characters and ordering by that value ascending.
SELECT *
FROM table_name
ORDER BY RIGHT(name, 3) ASC;
It should be added that as your data grows, this will become an inefficient solution. Eventually, you'll probably want to store the numeric appendix in a separate, indexed integer column, so that sorting will be optimally efficient.

you should try this.
SELECT * FROM Table order by SUBSTRING(name, -3);
good luck!

You may apply substring_index function to parse these values -
select * from table order by substring_index(name, '_', -1)

You can use MySQL SUBSTRING() function to sort by substring
Syntax : SUBSTRING(string,position,length)
Example : Sort by last 3 characters of a String
SELECT * FROM TableName ORDER BY SUBSTRING(FieldName, -3);
#OR
SELECT * FROM TableName ORDER BY SUBSTRING(FieldName, -3,3);
Example : Sort by first 3 characters of a String
SELECT * FROM TableName ORDER BY SUBSTRING(FieldName, 1,3);
Note : Positive Position/Index start from Left to Right and Negative Position/Index start from Right to Left of the String.
Here is the details about SUBSTRING() function.

If you want to order by the last three characters (from left to right) with variable name lengths, I propose this:
SELECT *
FROM TABLE
ORDER BY SUBSTRING (name, LEN(name)-2, 3)
The index starts at lenght of name -2 which is the third last character.
I'm a little late but just encountered the same problem and this helped me.

Related

MySQL - WHERE x IN ( column)

I tried something out. Here is a simple example in SQL Fiddle: Example
There is a column someNumbers (comma-seperated numbers) and I tried to get all the rows where this column contains a specific number. Problem is, the result only contains rows where someNumbers starts with the specific number.
The query SELECT * FROM myTable where 2 in ( someNumbers ) only returns the row with id 2 and not the row with id 1.
Any suggestions? Thank you all.
You are storing data in the wrong format! You should not be storing multiple values in a single string column. You should not be storing numbers as strings. Instead, you should have a junction table with one row per id and per number.
Sometimes, you just have no choice, because someone else created a really poorly designed database. For these situations, MySQL has the function find_in_set():
SELECT *
FROM myTable
WHERE find_in_set(2, someNumbers ) > 0;
The right solution, however, is to fix the data model.
While Gordon's answer is a good one, here is a way to do this with like
SELECT * FROM myTable where someNumbers like '2,%' or someNumbers like '%,2,%' or someNumbers like '%,2'
The first like checks if your array starts with the number you are looking for (2). The second one checks if 2 is within the array and the last like tests for appearance at the end.
Note that the commas are essential here, because something like '%2%' would also match ...,123,...
EDIT: As suggested by the OP it may happen that only a single value is present in the row. Consequently, the query must check this case by doing ... someNumbers = '2'
I would suggest this query :
SELECT * FROM myTable where someNumbers like '%2%'
It will select every entry where someNumbers contains '2'
Select * from table_name where coloumn_name IN(value,value,value)
you can use it

Is it possible in MySQL to search a table for where a column only contains 1 comma?

I have this column in a table which is comma delimited to separate the values.
Here's the sample data:
2003,2004
2003,2005
2003,2006
2003,2004,2005
2003,2007
I want to get all data that contains only 1 comma.
I've been playing around with the '%' and '_' wildcards, but I can't seem to get the results I need.
SELECT column FROM table WHERE column like '%_,%'
Replace the , with '' empty set then take the original length less the replaced length. if 1 then only 1 comma if > 1 then more than 1 comma.
The length difference would represent the number of commas.
Length(column) - length(Replace(column,',','')) as NumOfCommas
or
where Length(column) - length(Replace(column,',','')) =1
While this may solve the problem, I agree with what others have indicated. Storing multiple values in a single column in a RDBMS is asking for more trouble. Better to normalize the data and get it to at least 3rd Normal form!
You can also use find_in_set() method which searches a value in comma separated list, by picking the last value of column using substring_index we can then check result of find_in_set should be 2 so that its the second and last value from list
select *
from demo
where find_in_set(substring_index(data,',',-1),data) = 2
Demo
Maybe another solution is to use regular expression in your case it can look like this ^[0-9]{4},[0-9]{4}$ :
SELECT * FROM MyTable WHERE ColName REGEXP '^[0-9]{4},[0-9]{4}$'
Or if you want all non comma one or more time :
SELECT * FROM MyTable WHERE ColName REGEXP '^[^,]*,[^,]*$'

MySQL Select row where text field contains number bigger than X

I have a text column ( MISCDATA ) in a database wich contains multiple numeric values and string values, separated by comas and semicolons. Is there a way to build a query to select only the rows where MISCDATA contains a number bigger than 50 (example)?
I thought of the use of FIND_IN_SET() but I can't place it in the correct context.
SELECT * FROM Mytable WHERE FIND_IN_SET('NUMBER BIGGER THAN 50', MISCDATA);
I don't know if I explained myself correctly, anyhow all help will be apreciated. Thanks.
EDIT:
Some example data from the field:
MA, 22; HR, 42; HG, 29; JW, 44; MI, 76; GJ, 56;
The above example should be listed by the SELECT because it contains 2 numbers bigger than 50 (MI and GJ).
A solution would be to use REGEXP :
SELECT * FROM Mytable WHERE REGEXP '#yourRegexToWrite'.
The job is to write a good regex...

Mysql, how to order by the first number in a set of numbers

I need to order by a field that contains a set of numbers. Lets say a table named TEST contains ID, NAME, QUADS with QUADS as follows.
95,273,212,405
717,450,771,504
391,176,646,272
This are the results I am getting with a query such as
SELECT * FROM TEST ORDER BY QUADS
391,176,646,272
717,450,771,504
95,273,212,405
These are the results I am looking to get
95,273,212,405
391,176,646,272
717,450,771,504
I am only interested in the first number in the set for "order". Figure it might be possible with a substring to the comma but not sure how to do that in MySQL.
Try this:
select * from test
order by cast(substring_index(quads,',',1) as unsigned)
What you want is the substring_index function.
... order by substring_index(x_field,',',1)
This extracts the text in x_field up to the first occurrence of the comma delimiter
Try with this:
select QUADS, 0+QUADS as S from TEST order by S
0+QUADS will convert your string to int and will use for it just the first digits sequence before "," which is actually what you want.

How to order by a part of a column

i have field with content like this: 1, 2, 100 first two numbers can be any size third one can be up to 100.
Now i need to sort my fields by this third number but since i have 2 other numbers i don't know how to do it.
Maybe i could use something like REGEXP or something else?
So far i've tried SUBSTRING but since my two numbers can be any lenght something like
order by SUBSTRING(my_field, 4)
would work but if i have numbers like 32, 451, 30 it takes wrong numbers
Also i use php for to build my query, but i don't think it matters.
You can use SUBSTRING_INDEX. So something like:
SUBSTRING_INDEX(my_field, ',', -1)
EDIT: if you have spaces you might want to do some trimming as well.
ORDER BY SUBSTRING_INDEX(my_field,',',-1)+0 ASC -- or DESC
Use SUBSTRING_INDEX, which grabs the substring up to the nth occurence of the delimiter (in your case, comma), or, in the case of a negative n, it will return the substring after the nth occurence from the end.
To see what I mean try:
SELECT SUBSTRING_INDEX(my_field,',',-1)
FROM my_table
The reason there is a +0 in the ORDER BY is so that mysql sorts these as numbers, not strings.
This should work:
order by CONVERT(SUBSTRING(my_field, LOCATE(",", my_field, RIGHT)), INTEGER)
SELECT *
FROM (
SELECT
SUBSTRING(my_field, 4) AS my_field,
...
FROM
...
WHERE
...
) temp_table
ORDER BY my_field