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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:02:26+00:00 2026-06-05T08:02:26+00:00

I like to use self explaining names for associative selects, and sometimes it’s even

  • 0

I like to use self explaining names for associative selects, and sometimes it’s even mandatory to avoid duplicates, so I use the AS keyword alot. But it’s giving me some trouble with left joins.

This works:

$sql = "SELECT *,
        projects.id as projects_id
       FROM projects";

$sql .= " LEFT JOIN".
    " (SELECT 
        projectfiles.id as projectfiles_id,
        projectfiles.fileID as projectfiles_fileID,
        projectfiles.projectID as projectfiles_projectID
       FROM projectfiles
       ) AS projectfiles".
    " ON projects.id = projectfiles_projectID";

However now I end up with useless data from projects, because it also picks up the fields userID and name, which I don’t need. It’s also picking up the id twice.

So I tried changing it to;

$sql = "SELECT 
        projects.id as projects_id
       FROM projects";

With the ON line becoming

" ON projects_id = projectfiles_projectID";

But that gave the error Unknown column projects_id

So I tried

" ON projects.projects_id = projectfiles_projectID";

But still the same error

I then started experimenting, and tried (as a test)

$sql = "SELECT id,name,userID FROM projects";

$sql .= " LEFT JOIN".
    " (SELECT 
        projectfiles.id as projectfiles_id,
        projectfiles.fileID as projectfiles_fileID,
        projectfiles.projectID as projectfiles_projectID
       FROM projectfiles
       ) AS projectfiles".
    " ON projects.id = projectfiles_projectID";

And to my surprise, the LEFT JOIN didn’t seem to pick up anything at all.

Code:

$sql = "SELECT id,name,userID FROM projects";

$sql .= " LEFT JOIN".
    " (SELECT 
        projectfiles.id as projectfiles_id,
        projectfiles.fileID as projectfiles_fileID,
        projectfiles.projectID as projectfiles_projectID
       FROM projectfiles
       ) AS projectfiles".
    " ON projects.id = projectfiles_projectID";

$res = mysql_query($sql);
if(!$res) die(mysql_error());

if(mysql_num_rows($res) > 0)
{
    $rownum = 0;
    while($row = mysql_fetch_assoc($res))
    {
        print_r($row);
        echo "<br/><br/>";
    $rownum++;
    }
}

Output:

problem

Which is weird because there is only one row in projects but 3 in projectfiles with that projectID… what am I doing wrong?

  • 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-05T08:02:28+00:00Added an answer on June 5, 2026 at 8:02 am

    To select only from the projectfiles table:

    $sql = "SELECT projectfiles.*,
            projects.id as projects_id
           FROM projects";
    
    // rest of the code is the same
    

    Update

    $sql = "SELECT projectfiles.* FROM projects";
    // rest of the code is the same
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

PHP classes can use the keyword self in a static context, like this: <?php
I'd like to use a self.attr of a unittest.TestCase class, however it seems it
The following code works, but I'd like to use the self-invoking function syntax when
I would like to know if it is acceptable/preferred to use self::method() and parent::method()
I would like to use self defined Environment variables in my source code. I
I'd like to authenticate a user using ADFS 2.0 to use a self-written WCF
I have a task like this... def self.perform(parsed_json) do_hard_work use_faye_to_push_status rescue => exception use_faye_to_push_error
i would like use a variable session ($_session) but it doesn't work in Drupal
I like to use parameter for FILENAME in the code below, instead of N'D:\DBName.mdf'
I like to use the navigation history provided by Eclipse in my RCP Application.

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.