Muncher Posted November 24, 2008 Share Posted November 24, 2008 hey everyone, sample data +---+--------------+ | 1 + mouse food + +---+--------------+ | 2 + mouse pad + +---+--------------+ i have this query that i use from a search feature on my website. basically when someone searches for "mouse pad" it breaks up the phrase (using explode) into "mouse" and "pad" and runs a query for each word. so of course anything that is "mouse" or "pad" are the results, and they end up looking something like this Results for "Mouse" 1 mouse food 2 mouse pad Results for "Pad" 2 mouse pad so as you can see it duplicated 'mouse pad' because it contains more than one of the key words being searched for (mouse, pad). how do i compare the two query results and eliminate duplicates? here is the actuall query (nested inside a 'foreach' of the exploded search phrase). ABOUT THE VARIABLES IN MY EXAMPLE $findThis: is the search word/phrase that a user would enter. $searchBy: is a reference to the product category chosen by the user when searching. 'description2' and 'search' are columns in my data table that are being searched for the key words. //TRIM THE SEARCH PHRASE INTO INDIVIDUAL WORDS $trimmedfindThis = explode(" ",$findThis); //SEARCH FOR EACH INDIVIDUAL WORD ONE AT A TIME foreach ($trimmedfindThis as $trimm){ $query="SELECT * FROM products WHERE search_cat = '$searchBy' AND LOWER(description2) LIKE LOWER('%".$trimm."%') UNION SELECT * FROM products WHERE search_cat = '$searchBy' AND LOWER(search) LIKE LOWER('%".$trimm."%') ORDER BY name "; $result = mysql_query($query); $num_results = mysql_num_rows($result); //DISPLAYING THE RESULTS, I USE THIS FOR STATEMENT for ($i=0; $i <$num_results; $i++){ $row = mysql_fetch_array($result); FOR THIS POST I CUT OUT THE DISPLAY CODE. DISPLAYING IS NO PROB IM JUST SHOWING DUPLICATE RESULTS } there doesnt seem to be anything that i can add to the query, like DISTINCT or UNIQUE because the query is returning the proper results for each key word. i need to be able to compare the two queries and eliminate duplicates. thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/134093-removing-duplicates-not-so-easy/ Share on other sites More sharing options...
fenway Posted November 26, 2008 Share Posted November 26, 2008 You'll need to combine the results either before or after. Quote Link to comment https://forums.phpfreaks.com/topic/134093-removing-duplicates-not-so-easy/#findComment-699644 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.