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.
Related
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.
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 '^[^,]*,[^,]*$'
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 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?
I want to remove a spacial character in my query can anyone help. This is my query
select sum(value) from table_1 where id in (1, 2,);
This 1,2, is fetch from other table using sub-query.
To remove the trailing colon, you can use trim():
SELECT TRIM(TRAILING ',' FROM '1,2,');
My guess is that you want to look for individual values in the list, especially because ids don't usually contain commas.
For that, you can do:
select sum(value)
from table_1
where find_in_set(id, '1, 2,') > 0;
If the values are coming from a subquery, you would be better off using the subquery directly (in most cases). The query would be something like:
select sum(value)
from table_1
where id in (<subquery>);
You would need to modify the subquery to return a list of ids, rather than all concatenated into one field.