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 8024133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:46:22+00:00 2026-06-04T22:46:22+00:00

I am trying to query a MySQL database through Perl. My database has a

  • 0

I am trying to query a MySQL database through Perl. My database has a table whose primary key is
a composite of three columns PK1, PK2 and PK3. I have arrays arr1 and arr2 which contain some values
of PK1 and PK2 respectively. I want to find the corresponding value of PK3 in the database for every
possible combination of arr1 and arr2 (there may be some combinations for which there is no value
of PK3). For example:

PK1 PK2 PK3
"JIM"   "RED"   1
"JOHN"  "BLUE"  2
"ANN"   "GREEN" 3
"ANN"   "WHITE" 4
"ME"    "BLACK" 5
"ME"    "RED"   6

arr1 = ("JIM", "ME", "ANN")
arr2 = ("BLACK", "RED")

I should get the result:

arr3 = (1, 5, 6)

The best solution I could come up with was:

foreach my $el1 (@arr1) {
    $i = 0;
    $query = "SELECT PK3 FROM table WHERE PK1 = $el1 AND PK2 = ?;";
    foreach my $el2 (@arr2) {
        my $query_handle = $connxn->prepare($query);
        $query_handle->execute($el2);
        $query_handle->bind_columns(undef, \$rec);
        while($query_handle->fetch()) {
            push(@arr3, $rec); 
        }
    }
}

But this won’t work because there will be combinations of $el1 and $el2 for which there will be
nothing to bind. Is there any better/ faster way of doing this? Its a big table and the arrays are
also long, so my program would take time even if it does not give me an error. I am out of ideas here,
so any help will be appreciated. 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-06-04T22:46:25+00:00Added an answer on June 4, 2026 at 10:46 pm

    The query you want is:

    SELECT PK3 FROM my_table WHERE PK1 IN ('JIM','ME','ANN') AND PK2 IN ('BLACK','RED')
    

    See it on sqlfiddle.

    To construct it:

    my $query = sprintf(
      "SELECT PK3 FROM my_table WHERE PK1 IN (%s) AND PK2 IN (%s)",
      join(",", ('?')x@arr1),
      join(",", ('?')x@arr2)
    );
    my $query_handle = $connxn->prepare($query);
    $query_handle->execute((@arr1, @arr2));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to insert data in table in mysql database through php code
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 query a mysql table which places its date in the following
I am trying to query a MySQL database using webpy. From the SQL query,
I'm trying to download a range of rows from my MySql database, through a
I have folowing MYsql Query And Trying to right outer join but unable to
I trying to create a MySql Query from the $_POST values trouble is i
I'm trying to find a MySQL query which will obtain the time that is
I am trying to use this MySQL query: SET @a:=0; UPDATE tbl SET sortId=@a:=@a+1

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.