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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:53:46+00:00 2026-05-25T11:53:46+00:00

I made a query that is joining the same table – users- twice, with

  • 0

I made a query that is joining the same table – users- twice,
with JOINs.

SELECT  * from tbl1 
  JOIN  tbl2 ON tbl2.USER_ID = tbl1.ID 
  JOIN  tbl2 as alias ON tbl1.ID = tbl2.TEACHER_ID 
 WHERE  tbl1.ID = 45

The first call to that table is regular, and the second time I use an alias using AS.
When I get the result in phpMyAdmin, I see the results are not referred to as the alias, but with the usual name, that’s like this:

USERNAME => Ted, ID_NUMBER=>33322552, … ,USERNAME=>Josh ….

How can I read that associative array

  • 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-25T11:53:47+00:00Added an answer on May 25, 2026 at 11:53 am

    In this query, you are not using an alias, you are refering to a real table called tbl2, twice. This will not work.

    SELECT * from tbl1 
    JOIN tbl2 ON tbl2.USER_ID = tbl1.ID 
    JOIN tbl2 as alias ON tbl1.ID = tbl2.TEACHER_ID   
    //                              ^^^^ there's no alias here, it's still tbl2 
    WHERE tbl1.ID = 45
    

    Rewrite it to:

    SELECT * from tbl1 
    JOIN tbl2 AS t2A ON t2A.USER_ID = tbl1.ID 
    JOIN tbl2 AS t2B ON tbl1.ID = T2B.TEACHER_ID 
    WHERE tbl1.ID = 45
    

    The as keyword is optional.
    This way there can be no misunderstanding which version of tbl2 you are refering to.

    The difference between the two queries

    Note that you can also rewrite query 2 as:

    SELECT * from tbl1 
    JOIN tbl2 ON (tbl2.USER_ID = tbl1.ID) OR (tbl2.teacher_ID = tbl1.id)  
    WHERE tbl1.ID = 45
    

    In that case you would rewrite query 1 as:

    SELECT * from tbl1 
    JOIN tbl2 ON (tbl2.USER_ID = tbl1.ID) AND (tbl2.teacher_ID = tbl1.id)  
    WHERE tbl1.ID = 45
    

    See the difference?

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

Sidebar

Related Questions

So I made a query like this global $connection; $query = SELECT * FROM
I have a query that joins multiple tables. QString str1 = SELECT DISTINCT f.pk_file_ID,
I have made an SQL query that picks out web statistics from a database.
I made query that accepts two parameters. Now I want to use that query
I have a huge query that is being made dynamically, but I want the
I'm using PDO inside a database abstraction library function query that I've made. I'm
This is a follow on from another question i made I have this query
I have a query that I have made into a MYSQL view. This particular
I'd like a query that at its simplest will join 2 tables together with
*EDIT*** I've made great progress by combining the main query that pulls the airwaves

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.