Jump to content

Help with a LEFT JOIN pulling different users


coupe-r

Recommended Posts

Hi all,

 

I have a prop_notes table with created_by and updated_by.  Each are set to a users ID.

 

Here is the query:

 

$sql  = "SELECT pr.note, pr.updated_on, pr.updated_by, pr.created_on, pr.created_by, u.firstname, u.lastname FROM prop_notes pr ";
$sql .= "LEFT JOIN users u ON pr.created_by = u.user_id ";
$sql .= "WHERE pr.client_id = '".$_SESSION['client_id']."' AND pr.note_id = '".$page_note_id."' AND pr.deleteStatus = '0'";

 

I am joining on the created_by ID, which only gives me the created_by (firstname, lastname).  I need to be able to grab the firstname/lastname of both the created_by and the updated_by data.

 

Any ideas on how to make this query work?

Link to comment
Share on other sites

Hey, thanks man.

 

But the prop_notes table has the user_id in the created_by / updated_by fields.  I'm trying to join with the Users table to get the first and last names of each of those users.

 

Unfortunately, removing the LEFT doesn't work.

 

Thanks again.

Link to comment
Share on other sites

Hi, sorry i misread your problem, was not firing on all cylinders (too early) lol.

 

You might want to try using a nested select query. But be aware that if you are using a nested query the nested select can only return 1 row. Have a look at the query below and see if this puts you on the right tracks

 

SELECT pr.note, pr.updated_on, pr.created_on, CONCAT( u.firstname,' ', u.lastname) AS UpdatedBy,
(SELECT CONCAT( u.firstname,' ', u.lastname)  FROM prop_notes pr  JOIN users u ON pr.created_by = u.user_id   ) As createdBy
FROM prop_notes pr 
JOIN users u ON pr.created_by = u.user_id 
WHERE pr.client_id = '".$_SESSION['client_id']."' AND pr.note_id = '".$page_note_id."' AND pr.deleteStatus = '0'

Link to comment
Share on other sites

Thanks again.  The SQL you had gave me multiple subquery results, so I added another WHERE clause in the sub query and it works.  Let me know if this is correct.

 

SELECT pr.note, pr.updated_on, pr.created_on, CONCAT( u.firstname,' ', u.lastname) AS UpdatedBy,
(SELECT CONCAT( u.firstname,' ', u.lastname)  FROM prop_notes pr  JOIN users u ON pr.created_by = u.user_id
WHERE pr.client_id = '1' AND pr.note_id = '28' AND pr.deleteStatus = '0') As createdBy
FROM prop_notes pr
JOIN users u ON pr.created_by = u.user_id
WHERE pr.client_id = '1' AND pr.note_id = '28' AND pr.deleteStatus = '0'

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.