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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:53:05+00:00 2026-05-20T09:53:05+00:00

I am working on an inventory php/mysql application. I have a main table (inv_main)

  • 0

I am working on an inventory php/mysql application.

I have a main table (inv_main) which will hold the equipmentID and the userID whom actually holds this equipment.
I then have a equiptment table (inv_equip) which holds information about the piece of equipment as well as who ordered the product, and who it was ordered for, both linking back to userIDs.

I am trying to return a result of the equipmentID, ownerName, orderedByName, orderedForName

When I run the following query I get NULLs for orderedByName, and OrderedForName which should both return names as those fields have IDs and are not null.

SELECT inv_equip.tech_id, inv_user.user_lname as ownedByName, 
orderFor.user_lname as orderForName, orderFrom.user_lname as orderFromName
FROM inv_main
LEFT JOIN inv_equip ON inv_main.main_equip_id = inv_equip.equip_id 
LEFT JOIN inv_user ON inv_main.main_user_id = inv_user.user_id 
LEFT JOIN inv_user as orderFor ON inv_equip.equip_ordered_for = inv_user.user_id  
LEFT JOIN inv_user as orderFrom ON inv_equip.equip_ordered_from = inv_user.user_id   
WHERE (inv_main.main_equip_id = 26)

This is what I am getting for my output

tech_id                ownedByName  orderForName    orderFromName
TCH20110305_1299355914  admin     NULL            NULL

Now if i run this query I am getting the correct ownedByname again but the orderForName return i record for each user in my user table. The difference is I am not specifying to return a certain id but all in the db.

SELECT inv_equip.tech_id, inv_user.user_lname AS ownedByName, 
orderFor.user_lname AS orderForName, orderFrom.user_lname AS orderFromName
FROM inv_main
LEFT JOIN inv_equip ON inv_main.main_equip_id = inv_equip.equip_id
LEFT JOIN inv_user ON inv_main.main_user_id = inv_user.user_id
LEFT JOIN inv_user AS orderFor ON inv_equip.equip_ordered_for = inv_user.user_id
LEFT JOIN inv_user AS orderFrom ON inv_equip.equip_ordered_from = inv_user.user_id

I am getting the following results

                           OwnedBy OrderedFor   OrderedBy   
TCH20110304_1299257155  hucker  admin       NULL
TCH20110304_1299257155  hucker  hucker      NULL
TCH20110304_1299257155  hucker  beatty          NULL
TCH20110304_1299257155  hucker  Frank2          NULL
TCH20110304_1299257245  beatty  admin           admin
TCH20110304_1299257245  beatty  admin           hucker
TCH20110304_1299257245  beatty  admin           beatty

Where as I am looking for a sql query which results in

itemID  OwnedBy OrderedFor   OrderedBy   
 x      Hucker  Beatty       Frank

Any tips on how I could correct my join logic to return the names of the respected userID with only one record?

  • 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-20T09:53:06+00:00Added an answer on May 20, 2026 at 9:53 am

    Your 2nd and 3rd joins specify an on condition against the 1st join:

    LEFT JOIN inv_user 
    ON        inv_main.main_user_id = inv_user.user_id 
    LEFT JOIN inv_user as orderFor 
    ON        inv_equip.equip_ordered_for = inv_user.user_id  
    LEFT JOIN inv_user as orderFrom 
    ON        inv_equip.equip_ordered_from = inv_user.user_id 
    

    Try modifying the code so each join uses it’s own table:

    LEFT JOIN inv_user 
    ON        inv_main.main_user_id = inv_user.user_id 
    LEFT JOIN inv_user as orderFor 
    ON        inv_equip.equip_ordered_for = **orderFor**.user_id  
    LEFT JOIN inv_user as orderFrom 
    ON        inv_equip.equip_ordered_from = **orderFrom**.user_id 
    

    The ** are just there for emphasis.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im working with a mysql database via php. I have a table with some
So I'm working on a project for a realtor. I have the following objects/MySQL
I'm working on a player inventory system for a game. I have a struct
Working on a console application using Delphi 7, and have run into a problem.
Background I am working on a legacy small-business automation system (inventory, sales, procurement, etc.)
Here's the XML code I'm working with: <inventory> <drink> <lemonade supplier=mother id=1> <price>$2.50</price> <amount>20</amount>
Working on a project at the moment and we have to implement soft deletion
Working with python interactively, it's sometimes necessary to display a result which is some
I'm creating a game, and am currently working on the Inventory system for it.
I have developed an inventory management software using VB6. and i have copied it

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.