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
Related
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.
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
I'm using PHP but that's not important. I have a variable that contains a standard array of positive int IDs. And I want to do a SELECT query with this array against a MySQL table to find out what IDs are not already existing in the data table. My first thought was to use IN() but then I realized that doing it that way I can only get a list of IDs that do exist not ones that don't. Of course with a list of IDs that do exist, I could compile it into a second array and then use array_diff() but I can't help wondering if there's another way to do it.
I decided to use unset()
SELECT QUERY
where (row) {
unset(id)
}
I'm creating a search page for my application and its using MongoDB. So I need to search an array of strings in multiple fields.
When I search in a single field, I do this:
$docs = $collection->find(array('username' => new MongoRegex("/^query/"));
But when I search for multiple fields, what do I need to do? Something like this?
$docs = $collection->find(array('username','name', 'email' => new MongoRegex("/^query/"));
Here is a page that documents how to query MongoDB from PHP: http://php.net/manual/en/mongocollection.find.php
You may be thinking that because you want to compare multiple fields to the same value that there would be a special (shorter) syntax for it (just like when you want to compare a single field to multiple values, like in a range query).
However, that is not the case - you still have to list every field and that value you are comparing it to, as if each field was being compared to a different value.
The only other thing to consider is whether you are searching for all documents that have any of the fields match this regex or all of the fields match the regex. In the first case you have to do an "$or" query. See the last example on the bottom of the manual page for syntax.
One of my fields in my data base is an array which has been converted to a string using the implode() function.
How do I retrieve the contents of this field (LESSONS) from the database and store it to a string when a user entered value is equal to the the value of the field NAME?
Thanks in advance for any help you may be able to provide.
Here you go:
$r = mysql_query('SELECT LESSONS FROM TABLE WHERE NAME=\'user_string\'');
$rows = mysql_fetch_assoc($r);
echo $rows['LESSONS'];
I don't know if I understood your question but... Take a look about Stored Procedures
If you used the implode function to convert your array into a string, then this data has lost any information about the array keys.
As far as you want to convert the data back, use the explode function:
$array = explode(',', $columnData);
But You can therefore not search for array keys within the database.
Next to that, the MySQL database (I assume you're using MySQL) can not search for array keys anyway.
You need to store the data in some other way into the mysql to search for it in an SQL later on.
For example, you can create a table that stores key/value combinations with a grouping index.
However MySQL has some string functions that can help you searching within the (now) string data in the MySQL database.
When searching for a value, before the comparison add a comma at the beginning and one at the end of the string. There is a MySQL string function that can concatenate strings. Then search within that expression for your value with a comma added in front and back as well.
Then you can lookop a single array element within the mysql database. MySQL String Functions.
This is a quick solution only, this won't work on large databases performant. However it might solve your problem w/o changing your database structure.