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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:31:19+00:00 2026-06-12T20:31:19+00:00

I have two tables: table_1: item_id , name … table_2: image_id , item_id ,

  • 0

I have two tables: table_1: item_id, name … table_2: image_id, item_id, url

The following function retrieves the ‘urls’ of the items with the ‘$name’ that is passed in. I first need to get the ‘item_id’ from table_1 to use in the query. I only need to return the ‘url’s from table_2.

It currently works fine, but I feel it could be streamlined to be done in one query with a JOIN rather than two separate queries. I’ve tried using different ‘JOIN’s to make it work but I can’t seem to get it right.

Here’s the function as it stands now with the two queries…

function get_items( $name ) {

    global $wpdb;

    $sql1 = "SELECT `item_id` FROM `table_1` WHERE `name` = '$name'";
    $results1 = $wpdb->get_var( $sql1 );

    $sql1 = "SELECT `url` FROM `table_2` WHERE `item_id` = '$results1'";
    $results2 = $wpdb->get_results( $sql1, ARRAY_A );

    return $results2;

}

and here is the ‘JOIN’ that I tried implementing, but was unsuccessful (I’ve also switched it around and did a ‘LEFT/RIGHT JOIN’ as well resulting in the same result) …

$sql1 = "SELECT `table_2`.`url` 
         FROM `table_2` 
         INNER JOIN `table_1`
         ON `table_2`.`item_id` = `table_1`.`item_id`
         WHERE `table_1`.`item_id` = '$name'";

Any advice on combining these two queries?

  • 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-12T20:31:20+00:00Added an answer on June 12, 2026 at 8:31 pm

    The problem with your query is this WHERE table_1.item_id = '$name', it should be the name not the item_id

    SELECT  b.url
    FROM    table1 a
            INNER JOIN table2 b
                ON a.item_id = b.item_id
    WHERE   a.name = '$name'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables one with ID and NAME table 1 ID | NAME
So I have two tables. Table 1 is named 'appointment' with the following fields:
I have two tables user table user_id | name | 1 | peter |
In MySQL, I have two tables with a 1:n relationship. Table items has products,
I basically have two tables: A(id int, name varchar(10), info varchar(10)) B(id int, item
I have three tables: items: id name 4 Item Blue, L 2 Item Blue,
I have MySQL database with two tables: users and items. They look like this:
I have two tables: Table A: +----------+ | item_id | +----------+ | 10 |
I have two MySQL tables: One for items and another to log purchases. I'm
I'm currently facing a database query issue. I have two tables, items and details

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.