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.
Related
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)
I have a MySQL table with a varchar filed that has many records like:
folder/subfolder_1/file_xpto
folder/subfolder_2/file_abc
folder/subfolder_3/file_123
folder/subfolder_4/file_xyz
I would like in a single query to remove the portion of the string "/subfolder_x" so in the end it will be:
folder/file_xpto
folder/file_abc
folder/file_123
folder/file_xyz
How can I achieve this?
Use substring_index to get the first and last substrings and concatenate them using concat_ws.
select concat_ws('/',substring_index(colname,'/',1),substring_index(colname,'/',-1))
from tablename
where colname like 'folder/%'
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.
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?
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.