How to query something from an array using WHERE in sql - mysql

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')

Related

Filter records using a comma separated string

I've a string variable #IDS and I'm trying to filter the records from table user but nothing helping me out since I'm new to MySql.
SET #IDS = '1,2,3';
select * from user where find_in_set(#IDS,ID);
You need to switch the order of the parameters:
SELECT *
FROM user
WHERE FIND_IN_SET(ID, #IDS);
From the documentation:
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
In other words, the first parameter is the string you are trying to find in the CSV list of strings which is the second parameter. If ID can be found in #IDS, then the index (beginning at one) which matches will be returned. If no match be found, then zero will be returned.

Check if a country code exists in a cell

I'm trying to check if a country code exists in a cell country before a user can add anything to the database. So, if a country code did not exist, he would not be able to add anything.
I'm storing country codes as comma separated values. The column type is VARCHAR.
"id" "rtype" "country"
"1" "0" "US,UK,SE,CA"
"2" "1" "US"
"3" "2" "UK"
I ran the following query, but this results in no rows. Where am going wrong here? The problem is, this query needs to run with comma separated values as well as single values.
select id, rtype from test where id=1 and country in('US')
Expected results
id | rType
1 | 0
select id, rtype from test where id=2 and country in('US')
Expected results
id | rType
2 | 0
Try this out:
SELECT id, rtype FROM test
WHERE FIND_IN_SET('US', country) > 0
This is how the FIND_IN_SET(str,strlist) function works:
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. [...] Returns 0 if str is not in strlist or if strlist is the empty string.
MySQL has a really nice Set Datatype. It is very efficient and can store till 64 distinct members of 255 values. I don't know if you could change your column type, but it would solve a lot of problems:
your column would be indexable
you wouldn't store duplicated values by mistake
it would be stored very efficiently
invalid values would be ignored (or error if in strict mode)
trailing spaces are removed
You'd search using the FIND_IN_SET function.
Since the country codes are the comma separated values , you can not directly use in .
You can you like instead.
select id, rtype from test where country like '%US%'

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'

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

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);

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'