In my table, one of the column has comma separated values. I would like to select the entry based on one or two values if found in that column like
select * from table where tags contains ('ec2' or 'associate')
or contains multiple values
select * from table where tags contains 's3' and 'rds'
What is the right query?
You can use the in-built find_in_set function.
find_in_set('s3',tags) > 0 and find_in_set('rds',tags) > 0
You can use the like operator:
select * from table
where
concat(',', tags, ',') like '%,s3,%'
and
concat(',', tags, ',') like '%,rds,%'
if in tags after each , there is a space then:
select * from table
where
concat(', ', tags, ',') like '%, s3,%'
and
concat(', ', tags, ',') like '%, rds,%'
This could be done using mysql regular expression function REGEXP_LIKE :
REGEXP_LIKE(tags, '(^|,)\s*ec2\s*($|,)' )
AND REGEXP_LIKE(tags, '(^|,)\s*associate\s*($|,)' )
As it does need not modify the compared value, it might perform better than the solution using LIKE.
Regex explanation :
(^|,) : beginning of the string OR a comma
\s* : 0 or more consecutive spaces
ec2 : string to search for
\s* : 0 or more consecutive spaces
($|,) : end of the string OR a comm
If you want an OR filter instead of AND, it can be expressed in a single function call, like :
REGEXP_LIKE(tags, '(^|,)\s*((ec2)|(associate))\s*($|,)' )
Related
I have a table with a varchar column that represents a path. I want to search for rows that have a path that follow a pattern like name.name[*] where name can be anything. I am looking for repeated strings contained anywhere in the path column that are separated by a period and have a square bracket after them.
This seems to call for Regexp, so through python I have something like https://regex101.com/r/apS20a/4
However, trying to implement this with MySQL Regexp is not working. I have been able to translate the shorthand into REGEXP '([A-Za-z_]+).(\1[[0-9]+])', but it seems that MySql Regex does not support capture groups. Is there a way to accomplish what I am trying to do with mysql regexp? Thank you
I don't think that MySQL supports capture groups. But if you only have one example of .name[ in the string between the first . and the first [, you can hack your way around it. This is not a general solution, just a specific approach in this case.
You can get the name with:
select substring_index(substring_index(url, '[', 1), '.', -1) as name
And then incorporate this into a regular expression:
select t.*
from (select t.*,
substring_index(substring_index(url, '[', 1), '.', -1) as name
from t
) t
where url like concat('%', name, '.', name, '[%');
This just uses like instead of regexp, because [ and . are regular expression wildcards. Of course, this assumes that name does not have _ or %.
EDIT:
Here is a method that actually identifies when this occurs -- and works even if there are multiple patterns.
The idea is to construct the regular expression based on what happens between the . and [ -- and then to apply it. Delightfully self-referential:
select t.*,
(url regexp regex)
from (select t.*,
substr(regexp_replace(url, '[^.]*[.]([^\\[]*)\\[[^.]*', '|$1[.]$1\\\\['), 2) as regex
from (select 'abcde.de[12345.345[ABC' as url union all
select 'abcdefdef[[[[..123.124['
) t
) t;
Here is the above in a db<>fiddle.
I'm trying to find if any word of a given string is contained within a column in a mySQL table.
Example
String: 'Company LLC'
Column: 'LLC'
I've tried the below query but no dice.
select * from table
where column sounds like '%Company LLC%'
Try this:
select * from table
where column rlike replace('Company LLC', ' ', '|')
This does a regex match. In regex, the pipe char | means "or". The resulting regex means "Company or LLC". In MySQL, rlike matches when any part of the value matches (rather than the whole column matches), so you don't need ".*" on each end.
i have in my table places named field. there are space separated values(there are problem to store csv value in one field). now i want to fire query like below. how i can do ??
select * from tablename where variablename in places
i did try this way but it shows syntax error.
select * from tablename where variablename in replace(places,' ',',')
### places ###
bank finance point_of_interest establishment
Use FIND_IN_SET
For comma separated
SELECT *
FROM tablename
WHERE ( FIND_IN_SET( 'bank', variablename ) )
Refer : SQL Fiddle
For space separated
SELECT *
FROM tablename
WHERE ( FIND_IN_SET( 'bank', replace(variablename,' ',',') ) )
Refer : SQL Fiddle
The best solution would be to normalise your data structure and do not have a single field storing multiple values.
You can make a query work without normalisation, but any solutions would be lot less optimal from a performance point of view.
Use patter matching with like operator:
... where fieldname like '% searched_value %'
Use the replace() function and combine it with find_in_set():
... where find_in_set('searched_value',replace(fieldname,' ',','))>0
Hi I think your problem comes from the usage of IN
IN for MySql is used like this
SELECT *
FROM table_name
WHERE column_name IN (bank,finance,point_of_interest, establishment);
In case of you want to select places you need to specify each place into value like
I have a field where I save strings, like:
"one two-tree"
"one-two-tree"
"one-two tree"
"one two tree"
When I do a SELECT, I want to retrieve strings that have either "-" or " " (space). Example:
When I do:
Select name from table where name="one two tree"
I want it to bring also results where there is either space or -, in this case returning all string exemplified above.
Is there a wildcard for this?
As far as standard SQL, you must use "or", or "like". depending on what exactly you want. EXAMPLE: Select name from table where name like "one?two?tree".
However, mySQL supports a REGEX extension that will give you what you want:
http://dev.mysql.com/doc/refman/5.7/en/pattern-matching.html
One option is to use replace:
select name
from yourtable
where replace(name, '-', ' ') = 'one two tree'
There is, but it is slow: you can use REGEXP:
SELECT name
FROM table
WHERE name REGEXP 'one[- ]two[- ]tree'
or you can use replacements:
SELECT name
FROM table
WHERE REPLACE(name, '-', ' ') = 'one two three'
but your best bet is to make an additional column where you will have a normalised name (with dashes always replaced with spaces, for example) so you can take advantage of indices.
You can use the LIKE condition with '%' (wildcard operator) for this.
e.g.
SELECT name from table_name WHERE name LIKE '%-%' OR name LIKE '% %'
-- will return all names that have `-` or ` `.
I have values stored like this in a field 1,255,230,265.
Is there a function in MySQL that will give me the second value in that string? In this case it'll be 255.
I tried using locate, but that does not seem to be meant for this.
Try this
select SUBSTRING_INDEX(SUBSTRING_INDEX(field_name,',',2),",",-1) from table_name
You might want to use SUBSTRING_INDEX() function.
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(field,',',2),',',-1)
FROM yourTable.
This grabs everything infront of the second comma, then grabs everything after the last comma (-1)
Try This
select * from Table1 where ',' + Nos+ ',' like '%,255,%'
Refer:
MySQL query finding values in a comma separated string
mysql check if numbers are in a comma separated list
Use FIND_IN_SET() function:
SELECT *
FROM tableA
WHERE FIND_IN_SET(255, columnName)
OR
Use LIKE operator
SELECT *
FROM tableA
WHERE CONCAT(',', columnName, ',') LIKE '%,255,%'