| | |
MySQL Query - Searching for multiple keywords
Please support our MySQL advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Oct 2009
Posts: 1
Reputation:
Solved Threads: 0
Hello,
I have a two tables created to apply keywords to a given entry:
Table: File
FileID (int, pk, auto incr)
Name (varchar)
Table File_Keyword
KeywordID (int, pk, auto incr)
FileID (fk)
Keyword (varchar)
Given that I have a record in table File with 2 corresponding records in table File_Keyword assigning keywords "ABC" and "XYZ" to the given FileID.
What is a simple sql query to only return results if both strings are matched:
Any assistance would be appreciated.
Thank you in advance.
I have a two tables created to apply keywords to a given entry:
Table: File
FileID (int, pk, auto incr)
Name (varchar)
Table File_Keyword
KeywordID (int, pk, auto incr)
FileID (fk)
Keyword (varchar)
Given that I have a record in table File with 2 corresponding records in table File_Keyword assigning keywords "ABC" and "XYZ" to the given FileID.
What is a simple sql query to only return results if both strings are matched:
sql Syntax (Toggle Plain Text)
SELECT file.FileID WHERE file.FileID=file_keyword.FileID AND (file_keyword.Keyword LIKE '%ABC%' AND file_keyword.Keyword LIKE '%XYZ%');
Any assistance would be appreciated.
Thank you in advance.
Last edited by peter_budo; 25 Days Ago at 7:35 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
•
•
Join Date: Sep 2009
Posts: 6
Reputation:
Solved Threads: 0
0
#2 25 Days Ago
Hello, I had a similar problem recently, and solved it like this...
I still don't know if there's an easier way to do it.
MySQL Syntax (Toggle Plain Text)
SELECT A.FileID, A.Name FROM file AS A WHERE EXISTS ( SELECT B.FileID FROM file_keyword AS B WHERE A.FileID = B.FileID AND B.Keyword LIKE '%ABC%' AND EXISTS ( SELECT B.FileID FROM file_keyword AS B WHERE A.FileID = B.FileID AND B.Keyword LIKE '%XYZ%' ))
Regards, John
0
#3 24 Days Ago
You could also try using joins:
There must be a more dynamic way tho. A way to check multiple keywords without hard-coding in a sub-query or a join for each of them.
Damn... now I won't be able to think about anything else until I find it xD
sql Syntax (Toggle Plain Text)
SELECT f.FileID, f.Name FROM `File` AS f INNER JOIN `File_Keyword` AS fk1 ON f.`FileID` = fk1.`FileID` AND fk1.`Keyword` LIKE '%key1%' INNER JOIN `File_Keyword` AS fk2 ON f.`FileID` = fk2.`FileID` AND fk2.`Keyword` LIKE '%key2%'
There must be a more dynamic way tho. A way to check multiple keywords without hard-coding in a sub-query or a join for each of them.
Damn... now I won't be able to think about anything else until I find it xD
Please do not ask for help in a PM. Use the forums.
And use [code] tags!
And use [code] tags!
•
•
Join Date: Sep 2009
Posts: 6
Reputation:
Solved Threads: 0
0
#5 24 Days Ago
Here's my effort (still fairly new to PHP) ...
MySQL Syntax (Toggle Plain Text)
<?php $searches = array ("saloon", "diesel", "blue"); $fileID = 123; $count = 0; $alias = ""; $sql = "SELECT f.FileID, f.Name FROM `File` AS f "; foreach ($searches as $search) { $count ++; $alias = "fk".$count; $sql .= "INNER JOIN `File_Keyword` AS ".$alias ." ON f.`FileID` = ".$alias.".`FileID` AND ".$alias.".`Keyword` LIKE '%".$search."%' "; } $sql .= "WHERE f.`FileID` = ".$fileID; echo ($sql); ?>
Regards, John
0
#6 24 Days Ago
Ok, so I almost have this figured out.
There you can just list the keywords in the last NOT IN clause.
Only problem is, this returns files where all of it's keywords are in the list, but it doesn't have to match every keyword in the list.
For example, if I have created these rows:
Running the above query, it would return 'File1', even tho it only matches two out of three keywords.
sql Syntax (Toggle Plain Text)
SELECT f.`file_id`, f.`name` FROM `file` AS f WHERE f.`file_id` NOT IN( SELECT fi.`file_id` FROM `file` AS fi INNER JOIN `file_keyword` AS fki ON fi.`file_id` = fki.`file_id` WHERE fki.`keyword` NOT IN('first', 'second', 'third') );
php Syntax (Toggle Plain Text)
$keywords = array ("first", "second", "third"); $keyword_list = implode(", ", $keywords); $sql = <<<SQL SELECT f.`file_id`, f.`name` FROM `file` AS f WHERE f.`file_id` NOT IN( SELECT fi.`file_id` FROM `file` AS fi INNER JOIN `file_keyword` AS fki ON fi.`file_id` = fki.`file_id` WHERE fki.`keyword` NOT IN({$keyword_list}) ); SQL; echo $sql;
Only problem is, this returns files where all of it's keywords are in the list, but it doesn't have to match every keyword in the list.
For example, if I have created these rows:
sql Syntax (Toggle Plain Text)
INSERT INTO `file`(`name`) VALUES ('File1'), ('File2'); INSERT INTO `file_keyword`(`file_id`, `keyword`) VALUES (1, 'first'), (1, 'second'), (2, 'third'), (2, 'fourth');
Last edited by Atli; 24 Days Ago at 1:29 pm.
Please do not ask for help in a PM. Use the forums.
And use [code] tags!
And use [code] tags!
![]() |
Other Threads in the MySQL Forum
- Previous Thread: Problems restoring mysql database backups
- Next Thread: Stored Procedures Help please
| Thread Tools | Search this Thread |
access amazon api array autoincrement automation beginner button buyouts c# c++ cache cakephp calendar checkbox cms coldfusion compatible data database datagrid date display duplicates dynamic dynamically ec2 email error events exists forms google googleanalytics gui html if...loop ingres innerjoins insert java keyword keywords kickfire links linux mail mariadb marketing matching memory migrate mobile montywidenius msqli_multi_query multiple multipletables mysql mysql.data.client mysqlindex mysqlinternalqueries mysqlquery mysqlsearch news opendatabasealliance oracle organic parameter parsing php port ppc programming progressbar query ranking recourse search searchengineland searchwithoutkeywords select seo serps server simple solaris sorting sp speed sqlserver stored strings sun syntax table test unicode update upload virtualbox







