Jump to content

need help with join query/excluding? query?


The14thGOD

Recommended Posts

I'm having troubles figuring out how to do this (never done this before) and I think I'm also having a brain fart at the same time.

Basically I'm trying to grab people who are on a project, and exclude those from another query to get others who are tied to the client but not already on the project.

 

Here's my query so far..

SELECT DISTINCT n.id,n.name FROM (
SELECT u.id,u.client_id,u.name 
FROM users AS u 
RIGHT JOIN projects AS p ON p.client_id = u.client_id 
WHERE p.id = 4 AND u.status='enabled'
) AS n 
INNER JOIN project_relationships AS pr 
ON pr.user_id = n.id 
WHERE pr.project_id = 4 
AND n.id != pr.user_id

 

The results are nothing, the inner query:

SELECT u.id,u.client_id,u.name 
FROM users AS u RIGHT JOIN projects AS p 
ON p.client_id = u.client_id 
WHERE p.id = 4
AND u.status='enabled'

 

Results in both users tied to client:

id | name

1 | justin - THIS is the only user on the project

2 | jayjay

 

So basically, i want to grab jayjay only.

 

Sorry if I'm not explaining this well, I'm not exactly a mysql expert..

 

Thanks for any and all help,

Justin

 

*edited for readability*

 

Link to comment
Share on other sites

Users:

id | client_id | name

1 | 1 | justin

2 | 2 | laura

3 | 1 | jayjay

 

clients:

id | name

1 | client 1

2 | client 2

 

projects:

id | client_id | name |

1 | 1 | project 1

 

project_relationships:

id | user_id | client_id | project_id

1 | 1 | 1 | 1

 

There's a lot more columns, but nothing else that is related to this query in particular.

 

This query almost worked.. but it basically matches every other one (so it still pulls in both records), worked when 'justin' was assigned to the project and 'jayjay' wasn't. I'm assuming it can be written better though w/ less crap.

SELECT n.id,n.client_id,n.name 
FROM (
SELECT u.id,u.client_id,u.name 
FROM users AS u 
RIGHT JOIN projects AS p 
ON p.client_id = u.client_id 
WHERE p.id = 4 AND u.status='enabled'
) AS n 
INNER JOIN projects as j 
ON j.client_id = n.client_id 
INNER JOIN project_relationships AS pr 
ON pr.client_id = n.client_id 
WHERE pr.project_id = j.id 
AND pr.user_id != n.id

Link to comment
Share on other sites

I decided to drop what I had and try to approach it a bit differently, found a NOT IN clause, this seems to do the trick (for now knowing my luck...)

 

SELECT u.id,u.client_id,u.name

FROM users AS u

RIGHT JOIN projects AS p ON p.id = 4

WHERE u.id NOT IN (SELECT user_id FROM project_relationships WHERE project_id = 4)

AND p.client_id = u.client_id

AND u.status='enabled'

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.