mySQL: Trouble with select query containing | symbol (LIKE and/or REGEXP) - mysql

My current query looks like this:
$query = "SELECT * FROM t WHERE (data LIKE '$findme') OR (data LIKE '%$findme|%') OR (data LIKE '%|$findme%')";
The data in this column contains items that are delimited by pipe symbols ("|") and I need the query to find all records that contain any occurrences of an item in this field. Additionally, if there's only one item in this column, there will not be a "|" symbol present.
EXAMPLE:
$findme = "12";
QUERY SHOULD MATCH:
13|23|12
12
12|23
3|12|42
QUERY SHOULD NOT MATCH:
123|32
34|123
I'm not sure if using REGEXP would make this easier, but if so, any solution is welcome. Thanks!
SQLFiddle Example:
http://sqlfiddle.com/#!3/32afd/5

Using a combination of REPLACE() and FIND_IN_SET(), you can replace the | with commas, and locate your value $findme in the pipe-delimited set:
SELECT *
FROM table
/* Replace | with , and search in the set */
WHERE FIND_IN_SET('$find_me', REPLACE(data, '|', ',')) > 0
Note that this only works if the delimited values in data do not contain commas.
In the long run, the appropriate solution to this is to separate out the delimited column data into another properly normalized one to many table.

Related

search values using like clause on alias column

I have table which contain comma separated string I want to perform like query on 'name' column but 'name' is comma separated so it will not retrieve data easily so I am using replace to eliminate comma and than perform like query on alias column ,but It is not working.is there any way to perform like query on comma separated string
Table:
id name
i school,education
mysql query :
SELECT id,name, lower((REPLACE(name, ',', ''))) as test FROM `list`
where test like '%education%'
You should seriously avoid storing CSV data into single table columns as you are currently doing. That being said, here is one possible workaround:
SELECT id, name
FROM list
WHERE CONCAT(',', LOWER(name), ',') LIKE '%,education,%';
The idea behind the above trick is to build a CSV name string looking something like:
,A,B,C,D,
That is, every single name value is always surrounded by comma boundaries on both sides. Then, we only need to check that ,somename, be present in this CSV string.

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 query String has comma only once

I have got a table in one of the columns I got data separated by comma, I've been trying to figure out how I can make a query with MySQL that checks in this column that I got a name that has comma only once:
table
SELECT Name, num FROM biology WHERE Name = 'Yossi' AND contains (', ')=1 ;
So I would get only
table2
Thanks
You an use FIND_IN_SET() to find rows where the comma separated list contains a specific word.
There is no elegant way to count the rows with exactly two elements in the comma separated list, but one way to accomplish this is to compare the length of the string with commas to the length of the string with the commas removed. If removing all commas shortens the length by 1 character that means there is exactly one comma.
Something like this should work for you:
select *
from your_table
where find_in_set('Yossi',name)
and char_length(name) = (char_length(replace(name,',','')) + 1)

If value is present in stored text string

I have a table, one of the columns contains a text values, some of which are comma separated string, like this:
Downtown, Market District, Warehouse District
I need to modify my query to see is a given value matches this column. I decided that using IN() is the best choice.
SELECT *
FROM t1
WHERE myValue IN (t1.nighborhood)
I am getting spotty results - sometimes I return records and sometimes not. If there's a value in t1.nighborhood that matches myValue, I do get data.
I checked and there are no MySQL errors. What am I missing?
You can use FIND_IN_SET() to search a comma-delimited list:
SELECT *
FROM t1
WHERE FIND_IN_SET(myValue, REPLACE(t1.nighborhood, ', ', ','));
The REPLACE() is necessary to remove the extra spaces.
Another solution is to use regex to match your search value surrounded by commas if necessary:
SELECT *
FROM t1
WHERE t1.nighborhood REGEXP CONCAT('(^|, )', myValue, '(, |$)');
In general, it's bad design to store distinct values in a single column. The data should be normalized into a related table with a foreign key.

SQL - Query to find if a string contains part of the value in Column

I am trying to write a Query to find if a string contains part of the value in Column (Not to confuse with the query to find if a column contains part of a string).
Say for example I have a column in a table with values
ABC,XYZ
If I give search string
ABCDEFG
then I want the row with ABC to be displayed.
If my search string is XYZDSDS then the row with value XYZ should be displayed
The answer would be "use LIKE".
See the documentation: https://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html
You can do WHERE 'string' LIKE CONCAT(column , '%')
Thus the query becomes:
select * from t1 where 'ABCDEFG' LIKE CONCAT(column1,'%');
If you need to match anywhere in the string:
select * from t1 where 'ABCDEFG' LIKE CONCAT('%',column1,'%');
Here you can see it working in a fiddle:
http://sqlfiddle.com/#!9/d1596/4
Select * from table where #param like '%' + col + '%'
First, you appear to be storing lists of things in a column. This is the wrong approach to storing values in the database. You should have a junction table, with one row per entity and value -- that is, a separate row for ABC and XYZ in your example. SQL has a great data structure for storing lists. It is called a "table", not a "string".
If you are stuck with such a format and using MySQL, there is a function that can help:
where find_in_set('ABC', col)
MySQL treats a comma delimited string as a "set" and offers this function. However, this function cannot use indexes, so it is not particularly efficient. Did I mention that you should use a junction table instead?