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

  • SEARCH
  • Home
  • 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 6997203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:15:15+00:00 2026-05-27T20:15:15+00:00

I am trying to query a database. I already have a file that includes

  • 0

I am trying to query a database. I already have a file that includes some primary keys out of this whole database. Now I want to filter these primary keys and get only those primary keys which also agree to “other condition”. My primary keys are associated to abstracts in the database. The abstracts are full-text indexed. Now I want to consider the abstracts using the given primary keys, look for my “other condition(terms)” in those abstracts and if its present I want to pull their primary keys out(which is going to be same from the file). My “other condition” is another file with a list of terms. I want to get the abstracts that contain those terms within the given primary keys.

My full-text search is something like this:

while(<FILE1>){
$PK = $_;
foreach $foo(@foo){
my $sth = $dbh->prepare(qq{
   SELECT value
     FROM value_table
    WHERE MATCH (column_text_indexed) AGAINST (? IN BOOLEAN MODE)
}) AND primary_key=$PK;

$sth->execute(qq{+"$foo"});
}
}

where $PK is coming from the list of primary keys I already have.
$foo will be the list of the terms (condition 2) I am looking for.

Normally, I can run this query number of $PK times number of $foo. But I learned something about optimization by sub querying where I won’t be running my query # $PK times # $foo. That will get rid of inner loop but will still form combination of every $PK with every term in file 2 that is @foo. Something like as follows:

while(<FILE1>){
$PK = $_;
   my $sth = $dbh->prepare(qq{
   SELECT value
     FROM value_table
    WHERE MATCH (column_text_indexed) AGAINST (**SUB QUERYING HERE**)
}) AND primary_key=$PK;

$sth->execute(qq{+"$foo"});

}

Just I don’t know how to do it. I may be wrong with the syntax. I want to know how to write code for full-text search as well as a subquery. I hope this will be efficient than querying directly the combinations. Any help is greatly appreciated.

  • 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-27T20:15:15+00:00Added an answer on May 27, 2026 at 8:15 pm

    I don’t think you need to use a subquery. But you can still get rid of the inner loop by combining the match strings.

    my $against = join ' ', map {qq/"$_"/} @foo;
    while (my $PK = <FILE1>) {
        chomp $PK;
    
        my $sth = $dbh->prepare(qq{
           SELECT value
             FROM value_table
            WHERE primary_key = ?
              # no '+' so it can match against at least one of the words in the list
              AND MATCH (column_text_indexed) AGAINST (? IN BOOLEAN MODE)
        });
        $sth->execute($PK, $against);
    

    Update

    I revised it and have completely removed the query from the loops.

    my @primary_keys;
    while (my $PK = <FILE1>) {
        chomp $PK;
        push @primary_keys, $PK;
    }
    
    my $PK_list = join ',', map {qq/'$_'/} @primary_keys;
    my $against = join ' ', map {qq/"$_"/} @foo;
    
    my $sth = $dbh->prepare(qq{
       SELECT value
         FROM value_table
        # placeholders can only represent single scalar values so $PK_list can't be bound
        WHERE primary_key IN ($PK_list)
          # no '+' so it can match against at least one of the words in the list
          AND MATCH (column_text_indexed) AGAINST (? IN BOOLEAN MODE)
    });
    $sth->execute($against);
    
    # continue with fetching the rows
    ...;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Microsoft Access database query that I'm trying to import into a
I'm trying to query a database view that's not located on the same server
I'm currently trying to run a LINQ query over a MS SQL database. This
I'm trying to create a faster query, right now i have large databases. My
I have a class with a primary key that is stored in a database.
I already have this coded out (more or less) in Java for Linux OS,
I'm trying to query a MySQL database using an array but I'm having trouble!
I am trying to reconstruct a database query I created for MySQL in Microsoft
I'm trying to learn LINQ to SQL and I'm able to query the database
I'm trying to create a simple database table using the PHP MySQL query Create

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.