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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:19:49+00:00 2026-06-15T18:19:49+00:00

Possible Duplicate: How to LEFT JOIN two tables with the same column name I

  • 0

Possible Duplicate:
How to LEFT JOIN two tables with the same column name

I have this statement:

$sql = "SELECT * FROM deliveries AS del 
        LEFT JOIN job_sheet ON job_sheet.job_id=del.jID
        LEFT JOIN hierarchy AS h ON job_sheet.h_str=h.hID
        LEFT JOIN customers ON h.cust=customers.ID";

My problem is that the ‘customers’ table has a column called ID and so does the ‘deliveries’ table. The issue starts when I run a while loop to output results.

while($data = mysql_fetch_array($sql))
{
     echo $data['ID'];
}

it outputs the value of the last table that was joined… How can I separate the two? I mean, I need to be able to output ‘ID’ from both tables, but in the query, it’s the last table that wins. Renaming the column is not an option…

Any inputs?

  • 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-15T18:19:50+00:00Added an answer on June 15, 2026 at 6:19 pm

    I figured it out with some tweaking:

        SELECT 
           deliveries.*, 
           job_sheet.*,
           customers.ID as CUSTOMERID
           FROM deliveries 
              LEFT JOIN job_sheet ON job_sheet.job_id=deliveries.jID 
              LEFT JOIN hierarchy ON job_sheet.h_str=hierarchy.hID 
              LEFT JOIN customers ON hierarchy.cust=customers.ID
    

    Given that the only thing I needed from the customers table was an aliased ‘ID’ while keeping every other column from the other two tables.

    My while loop would ultimately change to:

    while($data = mysql_fetch_array($sql))
    {
        echo $data['CUSTOMERID']; // For the customer's ID
        echo $data['ID']; // For the delivery ID
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: SQL left join vs multiple tables on FROM line? SELECT messages.message_title, messages.message_content,
Possible Duplicate: SQL - how to SELECT multiple tables and JOIN multiple rows from
Possible Duplicate: How to join data frames in R (inner, outer, left, right)? This
Possible Duplicate: MySQL delete row from multiple tables I have 5 tables: members member_videos
Possible Duplicate: trim left characters in sql server? I have a query below that
Possible Duplicate: Combining rows of queried results by unique identifier? I have two tables
Possible Duplicate: How to check if mysql returns null/empty I have SQL command (LEFT
Possible Duplicate: What's the difference between NOT EXISTS vs. NOT IN vs. LEFT JOIN
Possible Duplicate: Why don't margin-top: auto and margin-bottom:auto work the same as their left
I need to select a row only from two corresponding tables, for instance, member

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.