How can I search within a table of comma-separated values? - mysql

I have a MySQL table which contains comma-separated values like this:
first row=(3,56,78,12)
second row=(6,44,2,3)
third row=(67,4,2,7,1)
fourth row=(88,55,22,33)
fifth row=(88,55,3,1,5)
I want to select the rows which have 3 in their set. How can I do this?

Try:
SELECT * FROM TABLE_NAME WHERE FIND_IN_SET( 3, COLUMN_NAME )

How about something like
SELECT *
FROM Table
WHERE Field LIKE '3,%'
OR Field LIKE '%,3'
OR Field LIKE '%,3,%'
OR Field = '3'

Just Use Mysql Function FIND_IN_SET(str,strlist) .
Returns a value in the range of 1 to N if the string str is in the string list strlist consisting of N substrings. A string list is a string composed of substrings separated by “,” characters. If the first argument is a constant string and the second is a column of type SET, the FIND_IN_SET() function is optimized to use bit arithmetic. Returns 0 if str is not in strlist or if strlist is the empty string. Returns NULL if either argument is NULL. This function does not work properly if the first argument contains a comma (“,”) character.
SELECT * FROM table_name WHERE FIND_IN_SET('3', column_name);

use this condition
WHERE firstrow LIKE '3,%'
OR firstrow like '%,3'
OR firstrow like '3'

SELECT *
FROM Table
WHERE Field IN ($first_row)
OR Field IN ($second_row)
OR Field IN ($third_row)
OR Field IN ($fourth_row);

you can use IN statement in your query.Please try this.
SELECT *
FROM Table
WHERE Field IN ($first_row)
OR Field IN ($second_row)
OR Field IN ($third_row)
OR Field IN ($fourth_row);

Related

mysql check if given string are in a comma separated list without FIND_IN_SET

SELECT * FROM TABLE_NAME WHERE 'string' in ('string1','string2','string3')
Its not given correct result find_in_set only given a exact result. If any way to get correct result without using find_in_set
SELECT * FROM TABLE_NAME WHERE 'string' in
('string1','string2','string3');
In the above query, string is supposed to be a field (column) so, if you were to have this query work, it would be without single quotes ', like this:
SELECT * FROM TABLE_NAME WHERE string IN
('string1','string2','string3');
This will return all rows where the field string is exactly string1 or string2 or string3.
Now, if you want to query the field string for values like "string", not exactly matching, then you use the LIKE operator:
SELECT * FROM TABLE_NAME WHERE string LIKE '%string%';
In the above query, all 3 records containing string1, string2 and string3 will be returned.

How to query something from an array using WHERE in sql

I tried the code below, but it does not work.
spark.sql("""SELECT categories, business_id
FROM business_data
WHERE categories = 'Ice Cream'
""").show(150, truncate=False)
It seems like there is a different way to query from an array but I cant figure it out.
This is what my data looks like.
Sample data:
Thank you
MySQL Specific:
FIND_IN_SET(str,strlist)
FROM DOCS:
Returns a value in the range of 1 to N if the string str is in the string list strlist consisting of N substrings. A string list is a string composed of substrings separated by , characters. If the first argument is a constant string and the second is a column of type SET, the FIND_IN_SET() function is optimized to use bit arithmetic. Returns 0 if str is not in strlist or if strlist is the empty string. Returns NULL if either argument is NULL. This function does not work properly if the first argument contains a comma (,) character.
mysql> SELECT FIND_IN_SET('b','a,b,c,d');
So in your case...
spark.sql("""SELECT categories, business_id
FROM business_data
WHERE Find_In_set('Ice Cream',categories)>1
""").show(150, truncate=False)
Normally if you want to query something out of an Array you would use array_contains, as such:
SELECT business_id, categories
FROM business_data
WHERE array_contains(categories,'Ice Cream & Frozen Yogurt')

find string in first 10 characters

I have a table with fields containing html code. I would like to select only those rows where htmlfield contain string "<p>[[{"fid":" but only in first 10 characters of the html field (this field contains more of such strings and I want to find only fields that contain the string in the beginning).
Is it possible to do such select?
You can use a SUBSTRING() to grab the fields with that string in them.
SELECT field1
FROM TableName
WHERE SUBSTRING(field1, 1, 12) = '<p>[[{"fid":'
Example
You can also try using the LIKE function. Where you can use the % wildcard at the end of your string to get fields that start with that string.
SELECT field1
FROM TableName
WHERE field1 LIKE '<p>[[{"fid":%'
Example
Do it like this:
select SUBSTRING(field_name,1, 10) from table_name;
It will display 10 characters only for that specific field.
Hope it helps

How to set the 'where' clause on a field with comma-seperated values?

I have a db table with a field with values stored in the value1,value2,value3,value4 format.
I want to find all the rows where this field contains a defined value, eg. value3.
How can I perform a query to search a value in a field like this one?
use FIND_IN_SET()
SELECT *
FROM tableName
WHERE FIND_IN_SET('value3', 'comma separated value here') > 0
SQLFiddle Demo
SOURCE
MySQL FIND_IN_SET
Description from MySQL Docs:
Returns a value in the range of 1 to N if the string str is in the
string list strlist consisting of N substrings. A string list is a
string composed of substrings separated by “,” characters. If the
first argument is a constant string and the second is a column of type
SET, the FIND_IN_SET() function is optimized to use bit arithmetic.
Returns 0 if str is not in strlist or if strlist is the empty string.
Returns NULL if either argument is NULL.
You can do this using like:
where concat(',', field, ',') like '%,value3,%'
SELECT *
FROM tableName
WHERE lower('comma separated values') LIKE 'value3'

MySQL SELECT WHERE 'a' IN (`field`)

I know it is not an appropriate technique to have a structure of MySQL table as such, but I have to work with such. The problem is, that the field in table has value with comma seperated integers, like "1,3,5,7,10" and I want the query to return rows, in which field has a to the query passed number in it, like:
SELECT * FROM `table` WHERE '5' IN (`field_in_table`)
However, it does not work, if, in this case, '5' is not the first number in the field.
Any advises, how to solve that?
Thanks in advance,
Regards,
Jonas
Have a look at
FIND_IN_SET
Returns a value in the range of 1 to N
if the string str is in the string
list strlist consisting of N
substrings. A string list is a string
composed of substrings separated by
“,” characters. If the first argument
is a constant string and the second is
a column of type SET, the
FIND_IN_SET() function is optimized to
use bit arithmetic. Returns 0 if str
is not in strlist or if strlist is the
empty string.
You could use WHERE field_in_table LIKE '%5%' instead.
Of course, the problem would be, '1,59,98' would return as wel.
SELECT * FROM table WHERE field_in_table LIKE '%5'");
should work
You could try
SELECT *
FROM table
WHERE '%,5,%' LIKE field_in_table OR
'%,5' LIKE field_in_table OR
'5,%' LIKE field_in_table;
A better approach might be to use regular expressions, a subject on which I am not an authority.
SELECT *
FROM table
WHERE FIELD LIKE '%,5,%' OR
FIELD LIKE '5,%' OR
FIELD LIKE '%,5'