Unable to search single number and comma separated string in same query - mysql

I am trying to search on specific scenario from last 8 hours but unable to sort it out.
I need to get records from single table where I need to match a comma separated string against two columns.
-Both columns contain single values like 1 or comma separated values like 1,2,3
I need to get records where minimum one AND condition matches for both columns either for single value or comma separated value.
Here is my query
SELECT specialities, ids_origin, id, latitude, longitude
FROM `ep_restaurant`
where `specialities` in (2,4,5,32) and `ids_origin` in (106,154,3)
The record is fetching
http://prntscr.com/ntiao7
But it is matching when both columns have same whole set of comma separated values, I also need to get all of those where even single values from both columns match like 2,6 or 2,154 or whole comma separated string like (2,4,5,32) and (106,154,3) matched..
It is not my own database so i cannot change it. Please help me .Thanks.

MySQL has a useful function for such must-avoid use-case.
For example, if you have specialities table:
SELECT r.id, latitude, longitude, r.specialities
FROM ep_restaurant r
WHERE exists( SELECT 1
FROM specialities s
WHERE find_in_set(s.id, r.specialities)
AND s.id in(2,6)
)
OR exists( SELECT 1
FROM <Origins Table> o
WHERE find_in_set(o.<Column Id>, r.ids_origin)
AND o.<Column Id> in(106,154)
)

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.

SELECT Numbers,Numbers AND str in mysql

I have a table in mysql that contain two columns at the coumn of contracts have three type of data number,string and(number&string together) how I can select only numbers and the fields that contain number&string without returning fields that only string by using regular expression the above is a capture of my table
Here you have
select * from contracts where contracts REGEXP '.*[0-9].*'

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.

Return rows with more than one comma separated value in a string

I have a table called contacts and in that table there is a field called contact_type.
contact_type is varchar and stores comma separated values in a sting like this:
^Media^,^Historical^
However only a few rows out of thousands have more than one value stored and I need to to run a query that will return only the rows with more than one so if it stores just ^Historical^ then it will be ignored.
I’m pretty much stumped on how to build a query like this. I assume it will contain something like this:
SELECT LENGTH(#css) - LENGTH( REPLACE( #css, ',', '') ) + 1;
Basically you need to select the records where contact_type contains a comma
select * from your_table
where instr(contact_type, ',') > 0