How to search for comma separated multiple values - php

I have created a table in MySQL in which a "skill" field has multiple comma-separated values, for instance: "PHP, JavaScript, C#".
I want to know an SQL query which will go through all values and give back a result according to the search values.

The correct MySQL function is FIND_IN_SET().
You would use it like:
SELECT * FROM USERS WHERE FIND_IN_SET('PHP', skill)>0
Where you can swap PHP out for the string you are searching for. This isn't the most efficient way to organize this data, but for smaller data sets might be fine.

Related

How to check if a DB array matches any values of a given array

I'm a little bit stuck.
I have an SQL column that contains weather codes (like Rain, Snow, etc.)
It is comma separated so the column would have a value of something like
rain,snow,haze
Now, I want to select the rows that contain values from an array.
I have an SQL code that is something like this:
SELECT * FROM locations WHERE currentWeather IN ('rain', 'snow', 'cloudy') ORDER BY name ASC
The problem is that this obviously works when currentWeather column only contains one item.
Is there a way to do it so that if the column value contains any of the items from the given array, it selects it?
Also, would it select it twice if two items match?
Best wishes
Use unnest in a subselect.
Select distinct A.myArray from (select unnest(column) as myArray from table) A where A.myArray in (your words to filter for)
Notice that using arrays in sql isn't very ideal and does not follows normalization rules. Your tables should ideally not contain arrays but rather just several rows each one containing the specific value you Want. It prevents issues such as this one.
To avoid the selection of repeated values, use the Distinct keyword right after you write select.
Rsference:
https://www.w3resource.com/PostgreSQL/postgresql_unnest-function.php
WHERE FIND_IN_SET(currentWeather, "rain,snow,cloudy")
Picks apart the string at commas (only) to see if currentWeather is any one of those 3 'words'.
See also FIELD(...)

search exact string exist within column mysql

I'm using below sql query for search values from db, but when I search 'study' it will returns the values 'caese-study', 'get-study-materials' as well.
How can I use a query to search exact contains withing string column?
$names = 'study';
and the names column has values like comma separated,
Ex: 'study, abc, new' so I need to search within that too
SELECT * FROM datatitle WHERE names LIKE '%$names %' ;
SELECT * FROM datatitle WHERE names regexp '(^|[[:space:]])$names([[:space:]]|$)';
I try with above two queries but didnt work as expect, pls advice?
You should not be storing comma-separated values in a column. You should be using a junction/association table. You should fix the data model, if you can.
However, sometimes we cannot control other people's really bad decisions. MySQL has find_in_set():
SELECT dt.*
FROM datatitle dt
WHERE find_in_set(?, names) > 0;
Note that I have replaced the constant $names with a parameter. You should learn to use parameters to pass values into queries.

mysql WHERE multiple values

i have a Problem using WHERE in my Select query.
The DB-table contains a field that stores data divided by a comma like "1,2,3".
I dont know how to check if the field contains 1,2 or 3. The usual query would be
"SELECT * FROM ".$table." WHERE name = '".$val."'".
But of course this only finds entries that equal $val, so if I search for "1" it will only give me the entries only containing "1", not "1,2,3" or "1,2". Is there a way to do that?
Thanks guys
You should fix your data structure. There are lots of good reasons to avoid storing lists of numbers as strings:
Values should be stored as their correct data types. Numbers are not strings.
SQL has pretty poor string processing functionality.
SQL has this great data structure for storing lists. It is called a table.
If the numbers refer to another table, then you cannot declare proper foreign key relationships.
Sometimes, we are stuck with other people's really bad design decisions. In those cases, MySQL offers find_in_set():
where find_in_set(1, name) > 0

check if exists in two coma separated values mysqli

I am having an issue where I have a table in a database which stores values in this form 1,3,4,55,6,22,44 and I have an array which is dynamic from the client side it can be like 2,55,33,1,33,99
I want to make a query to select this field if the at least any value matches between the fields.
Like select the field if in the database value there exists 24 and I have 23,24,55,66 from the user array
I think this can be done with find_in_set() or the IN keyword
It is not a good practice to create comma seperated values in a column. Usually people feel that it was the easiest and simplest method, but it is not. Searching and modification will be so hard. Find set can be used if you have a single item to search ; In your case you can do it like this. Implode the array with | and use it in regular expression.
<?php
$arr = Array(2,55,33,1,33,99);
echo 'select * from table where CONCAT(",", `field`, ",") REGEXP ",('.implode("|",$arr).'),"';
?>
For comparing two arrays, you should use:
array_intersect()
after converting table data into array as well.
Reference

Compare Mysql Field Value

I have a table named 'jobs' with field 'categories'.
Now Values in my category field is like this
368,369,372,379,380,381
368,369,372
369,373,374,375,376
491,492
& i am getting values with search like this
'368','374','490'
Now how to compare myvalue to field value, I can't use "IN" in this stage.
In above case,
If i have values like "'368','374','490'" this, i want first three rows, beacause it contains atleast one value from my list.
how can i do this? Please suggest.
if you happy to use in programming end (i.e. using php) , you can pull those database data into an associative array. Then you can easily that array for your data manipulation. In php there are many inbuilt functions for arrays. you can use those.
http://de3.php.net/manual/en/language.types.array.php
http://www.php.net/manual/en/ref.array.php

Categories