Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I search for an a list of words in a table of words that associate to another table?
    primarykey
    data
    text
    <p>Below are 3 tables I have setup. The 'words' and 'word_part_mapping' tables are generated automatically by indexing all of the words in the part's name from the parts table.</p> <pre><code>//'parts' table +----------+--------------+ | part_num | part_name | +----------+--------------+ | 10111 | front bumper | | 10112 | rear bumper | +----------+--------------+ //'words' table +------+------------+ | id | word | +------+------------+ | 1 | front | | 2 | bumper | | 3 | rear | +------+------------+ //'word_part_mapping' association table +---------+----------+ | word_id | part_num | +---------+----------+ | 1 | 10111 | | 2 | 10111 | | 3 | 10112 | | 1 | 10112 | +---------+----------+ </code></pre> <p>How can I query this if the user searches for "front bumper" and I want the query to return results containing ALL words that the user searched for?</p> <pre><code>SELECT p.* FROM words w LEFT JOIN word_part_mapping wpm ON w.id=wpm.word_id LEFT JOIN parts p ON wpm.part_num=p.part_num WHERE w.word='front' AND w.word='bumper' </code></pre> <p>Obviously, the above query does not work because the word cannot equal both 'front' and 'bumper'. This works if I do OR, however I do not want that because it returns too many results (50,000+ parts in database).</p> <p>==============================================</p> <p>EDIT: Got it working finally... </p> <pre><code>SELECT p.* FROM words w LEFT JOIN word_part_mapping wpm ON w.id=wpm.word_id LEFT JOIN parts p ON wpm.part_num=p.part_num WHERE w.word IN('front','bumper') GROUP BY p.part_num HAVING COUNT(DISTINCT w.word) = 2 </code></pre> <p>where 2 is the number of terms the user is searching</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload