Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4322244
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T08:50:55+00:00 2026-05-21T08:50:55+00:00

I have this query: $relevantwords = {one , two , three } ; foreach

  • 0

I have this query:

$relevantwords = {"one" , "two" , "three" } ;

foreach ($relevantwords as $word)
     {
        $query .=  "SELECT * FROM delhi_items WHERE heading like '%$word%' AND id!={$entry_row['id']} AND visible=1 UNION ALL "  ; 
     }
$query = implode( " " , explode(" " , $query , -3) ) ;                                                

      $query .= " ORDER BY time_stamp DESC LIMIT 0, 20 " ;
     $result_set = mysql_query($query, $connection);

This causes several duplicates in my resultset. Is there a way to detect and remove these duplicates from the resultset ? I know I should probably try to avoid the duplicates in the first place, but I am unable to figure that out.

Also I tried distinct keyword, it didn’t work (because its a loop, the same entry is fetched again and again).

Laslty I am kind of an amateur so please tell me if I am doing something fundamentally uncool with such a long sql query in a for loop.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T08:50:56+00:00Added an answer on May 21, 2026 at 8:50 am

    I would try to have one SELECT and no UNION and DISTINCT. It will probably be a faster query:

    $relevantwords = {"one" , "two" , "three" } ;
    
    $querycondition = "" ;
    foreach ($relevantwords as $word)
         {
            $querycondition .= " heading LIKE '%$word%' OR"
         }
    $querycondition = substr($querycondition ,0 ,strlen($querycondition)-2 ) ;
    
    $query = " SELECT * "
           . " FROM delhi_items "
           . " WHERE ( "
           .           $querycondition 
           . "       ) "
           . "   AND id!={$entry_row['id']} "
           . "   AND visible=1 "
           . " ORDER BY time_stamp DESC "
           . " LIMIT 0, 20 " ; 
    $result_set = mysql_query($query, $connection);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this query: SELECT * FROM table WHERE key LIKE '1,2,3,%' OR key
I have this query: select * from table where column like '%firstword[something]secondword[something]thirdword%' What do
I have this query: SELECT page.id, revision.title, revision.number FROM page INNER JOIN revision ON
I have this SQL query: SELECT * FROM IMAGES WHERE IMAGENAME in ('IMG1', 'IMG2',
I have this query with MySQL: select * from table1 LIMIT 10,20 How can
I have this query: SELECT p.id, r.status, r.title FROM page AS p INNER JOIN
I have this query SELECT id, timeOrdered, order_desc, firstname, lastname FROM `db`.`myTable` WHERE `myTable`.`customer_number`
I have this query at the moment SELECT year_start_1 FROM table1 But i need
I have this query: SELECT DISTINCT id, label FROM bbproduct_cust WHERE CUSTNO IN (SELECT
I have this query: SELECT to_number(gr.code) group_index, gr.NAME group_name, f.*, gr.description gr_desc FROM obj$groups

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.