Jump to content

Recommended Posts

Server version: 5.5.23 - MySQL Community Server
 

I have a table containing project assignments represented by a projectid column and a user_assignment column which contains the user's unique userid. A user is assigned to a project if they have a record in the user assignment table associated with the project id. I want retrieve all the projects that a specific user is NOT assigned to without getting duplicate records since there are many users assigned to projects.

 
While these examples will retrieve only one record per project, it returns projects that user 'abc123' is and is NOT assigned to. I need to retrieve the project ids that the user is NOT assigned to.
SELECT DISTINCT `propid` 
FROM `user_assignments` 
WHERE `userid` <> 'abc123' 
ORDER BY `propid` ASC


SELECT DISTINCT `propid` 
FROM `user_assignments` 
WHERE (`userid` <> 'abc123') 
ORDER BY `propid` ASC

I also tried a sub-query but it locks up mySQL in phpMyAdmin and never returns any records which is telling me there is something really wrong with this.

SELECT DISTINCT `propid` 
FROM `user_assignments` 
WHERE `propid` NOT IN (SELECT `propid` 
FROM `user_assignments` 
WHERE `userid` = 'abc123')
ORDER BY `propid`

I have limited experience with writing SQL so I would really appreciate any recommendations. I am sure there is a very simple solution but I am not seeing it.

Edited by JipThePeople
Link to comment
https://forums.phpfreaks.com/topic/287654-retrieving-distinct-records/
Share on other sites

The column is propid and it's in the user_assignments table?

 

You don't need to DISTINCT anything. Select from the projects table (the one that has a list of all the projects) then LEFT JOIN it with the assignments table on the matching project ID and the correct user ID, then keep everything that failed to join.

SELECT p.id
FROM projects p
JOIN user_assignments ua ON p.id = ua.propid AND ua.userid = 'abc123'
WHERE ua.id /* or another column in that table */ IS NULL
If you're getting poor performance out of the query, that's a different problem...

 

[edit] Above should say LEFT JOIN. Like I did in the description before it.

Edited by requinix

The column is propid and it's in the user_assignments table?

 

You don't need to DISTINCT anything. Select from the projects table (the one that has a list of all the projects) then LEFT JOIN it with the assignments table on the matching project ID and the correct user ID, then keep everything that failed to join.

SELECT p.id
FROM projects p
JOIN user_assignments ua ON p.id = ua.propid AND ua.userid = 'abc123'
WHERE ua.id /* or another column in that table */ IS NULL
If you're getting poor performance out of the query, that's a different problem...

 

 

Thanks for your response. I'm not sure a JOIN is required since all the data required is in the user_assignments table. I need a list of propids where user 'abc123' does not have a matching record for that project. All that info is contained in the user_assignments table.

 

Here is the user_assignments table.

CREATE TABLE IF NOT EXISTS `user_assignments` (  `id` int(10) 
unsigned NOT NULL AUTO_INCREMENT,  `userid` varchar(100) NOT NULL 
DEFAULT '',  `propid` int(10) unsigned NOT NULL DEFAULT '0',  
`security_level` int(10) unsigned NOT NULL DEFAULT '0',  `role` int(10) 
unsigned DEFAULT '0',  `custom_security` int(10) unsigned DEFAULT 
'0',  PRIMARY KEY (`id`)) ENGINE=MyISAM  DEFAULT 
CHARSET=latin1 ;

I ran the query you suggested and it returned an empty set which is not correct because there are projects not assigned to user 'abc123'. Does this additional information clarify my problem a little more? I really appreciate you taking the time to review my issue.

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.