MySQL Select when specific digit is x - mysql

I have a column that contains values like 5A898, 89KAS, 89ASD.
I'm trying to write a query that will only return rows where the third digit of the column is 'A'. For instance '89ASD' would be returned but '89KAS' would not. I'm not sure what the right way to go about this is. Regular expressions?
So...
SELECT column
FROM table
WHERE column = ?

WHERE column LIKE '__A%'
^^-- 2 underscores
should do the trick. two "whatever" characters, followed by an A, then followed by anything in any amount.

Maybe you can use MySQL's SUBSTRING
SELECT column
FROM table
WHERE SUBSTRING(column,3,1) = 'A'

where right(left(col,3),1) = 'A'
That can help... MarcB's answer is cleaner

You can do this with a regex, but I think it might be easier just coping with a string operation here:
SELECT column
FROM table
WHERE SUBSTRING(column,3,1) = 'A';

Related

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 '^[^,]*,[^,]*$'

SELECT rows that contain specific alphanumeric characters in MySQL

I'm trying to select all rows that contain only specific alphanumeric characters in MySQL using:
SELECT * FROM table WHERE column LIKE '%abc%' OR column LIKE '%abd%'
OR column LIKE '%ab%'
For instance:
abc1234 is ok
abd1234 is ok
abe1234 is not ok
abg4567 is not ok
ab1234 is ok
ac1234 is not ok
The problem is, it select all the "abe","abg". How to select abc1234, abd1234 and ab1234?
With this
OR column LIKE '%ab%'
As part of the WHERE clause, it's no surprise that abe and abg are selected.
Please permit me to also mention that queries LIKE '%something%' cannot make use of any indexes and are likely to be very slow on large tables. The fact that you have three of them in one query is only going to make it worse.
Use the below Query which will also use indexes on column column
SELECT * FROM table WHERE column LIKE 'ab%' AND column not LIKE 'abe%'
AND column not LIKE 'abg%'
The Reason the indexes will be used in this case is the query is not using wildcard in the beginning of the string literal
Remove the last part of your query, it is telling it to select abg and abe.
You have told it to select all items beginning with anything and ending with anything providing ab is in the middle.
Because column LIKE '%ab%' will get "abe" or "abg"
If you want number after "ab", this is your query:
SELECT * FROM table WHERE column LIKE '%abc%' OR column LIKE '%abd%'
OR column LIKE '%ab0%' OR column LIKE '%ab1%' OR column LIKE '%ab2%' OR column LIKE '%ab3%' OR column LIKE '%ab4%' OR column LIKE '%ab5%' OR column LIKE '%ab6%' OR column LIKE '%ab7%' OR column LIKE '%ab8%' OR column LIKE '%ab9%'
Are you going to select column contain "abc" or "abd" or "ab" but not "abe"or "abg". For this case i don't think you can use LIKE. Try to use REGEXP. i think "ab[^g^e]?" should do the job for you.
SELECT name FROM table WHERE column REGEXP 'ab[^g^e]?';

mysql - search pattern and replace

I have MySQL table wp422_posts and I need to manipulate one type of values in post_name column. Actually, I need to manipulate long value beginning with 4 digits (unique ID) and than text, I need to get the 4 digits and left them there. Could anybody help me, please? I'm new to MySQL. Other values - which do not start with 4 digits and dash has to stay same.
one example of value of post_name
old:
"2147-sprava-uzivatelskych-uctu-databaze-oracle-v-prostredi-autocad-map-3d"
desired
new one: "2147"
How to select correct values in column:
SELECT * FROM `wp422_posts` WHERE `post_name` LIKE "[0-9][0-9][0-9][0-9]%"
it returns empty result, but why?
How to get rid of not needed part?
Thanks relly much for your help.
If you are sure that starting part is always 4 digit number and that is only needed, then you can use this:-
SELECT LEFT(COL_NAME, 4)
FROM `wp422_posts`
Then you can use update statement to update the table. Somthing like:-
UPDATE `wp422_posts`
SET COL_NAME = LEFT(COL_NAME, 4)
Hope this helps!!

MySQL multiple IN clause does not work

I have a MySQL table column rubrics which contains string value '61,80,112,256'. So I try execute that query:
select * from table where 256 in (rubrics) and 61 in (rubrics)
And no result returns. Any suggestions?
Since your rubrics column is a comma separated list the IN operator will not work.
MySQL does have a function that can find a value in a string list so you should be able to use FIND_IN_SET():
select *
from yourtable
where find_in_set(61, rubrics)
or find_in_set(256, rubrics)
See SQL Fiddle with Demo
Something like WHERE rubrics LIKE '%,256,%' OR rubrics LIKE '256,%' OR rubrics LIKE '%,256'. Using parenthesis you can also filter on the 61, but the resulting query will be come messy. You'd better normalize your data, so you can use subqueries and joins using keys (the real deal).
(see also bluefeet's answer as FIND_IN_SET is a better approach)
Try this
select * from table where rubrics like '%'+'256,'+'%' and rubrics like '%'+'61,'+'%'
IN operator does not work with strings
use correct substring matching operator like LIKE or LOCATE
one advice - update your rubics column to begin and end with , character, that will make your LOCATE(",62,", rubics) operations unambiguous as opposed to LOCATE("62", rubics) which will match also 622 and 262 and other combinations. Locating ,62, wil fail if your rubics has value of 62,blah,foo,bar because it doesn't start with ,

MySQL SELECT WHERE 'a' IN (`field`)

I know it is not an appropriate technique to have a structure of MySQL table as such, but I have to work with such. The problem is, that the field in table has value with comma seperated integers, like "1,3,5,7,10" and I want the query to return rows, in which field has a to the query passed number in it, like:
SELECT * FROM `table` WHERE '5' IN (`field_in_table`)
However, it does not work, if, in this case, '5' is not the first number in the field.
Any advises, how to solve that?
Thanks in advance,
Regards,
Jonas
Have a look at
FIND_IN_SET
Returns a value in the range of 1 to N
if the string str is in the string
list strlist consisting of N
substrings. A string list is a string
composed of substrings separated by
“,” characters. If the first argument
is a constant string and the second is
a column of type SET, the
FIND_IN_SET() function is optimized to
use bit arithmetic. Returns 0 if str
is not in strlist or if strlist is the
empty string.
You could use WHERE field_in_table LIKE '%5%' instead.
Of course, the problem would be, '1,59,98' would return as wel.
SELECT * FROM table WHERE field_in_table LIKE '%5'");
should work
You could try
SELECT *
FROM table
WHERE '%,5,%' LIKE field_in_table OR
'%,5' LIKE field_in_table OR
'5,%' LIKE field_in_table;
A better approach might be to use regular expressions, a subject on which I am not an authority.
SELECT *
FROM table
WHERE FIELD LIKE '%,5,%' OR
FIELD LIKE '5,%' OR
FIELD LIKE '%,5'