esport Posted October 15, 2008 Share Posted October 15, 2008 Hi Guys, Having troubles trying to produce a query. I want to display all users form the user table, but only display the users that are not assigned a project in the project table. How do I do this? I have tried using LEFT JOINS, but I think I may need to do inner queries. user table user_id name 1 Dan 2 Joe 3 Ash 4 Ben project table user_id project_id 1 2 3 2 So in this example the results should only display Joe and Ben Thanks in advance for any help. d Quote Link to comment https://forums.phpfreaks.com/topic/128611-solved-how-to-display-all-from-one-table-but-only-if-they-dont-exsist-in-another/ Share on other sites More sharing options...
Barand Posted October 15, 2008 Share Posted October 15, 2008 For this type of query you use a left join. Where there is no matching project, null values are returned in cols from that table SELECT u.name FROM user u LEFT JOIN project p USING (user_id) WHERE p.user_id IS NULL see http://www.phpfreaks.com/tutorial/data-joins-unions Quote Link to comment https://forums.phpfreaks.com/topic/128611-solved-how-to-display-all-from-one-table-but-only-if-they-dont-exsist-in-another/#findComment-666538 Share on other sites More sharing options...
esport Posted October 15, 2008 Author Share Posted October 15, 2008 Thanks for the quick response. Im assuming where you have USING (user_id) the user_id is from the user table or the project table? Do I need to add a prefix? I didnt seem to work. Also if I just want to check a certain project I just add in WHERE p.project_id = $project_id Quote Link to comment https://forums.phpfreaks.com/topic/128611-solved-how-to-display-all-from-one-table-but-only-if-they-dont-exsist-in-another/#findComment-666563 Share on other sites More sharing options...
Mark1inLA Posted October 15, 2008 Share Posted October 15, 2008 Or to take another approach (not sure what you guys think of sub-queries): SELECT users.name FROM users WHERE users.user_id NOT IN (SELECT projects.user_id FROM projects) Quote Link to comment https://forums.phpfreaks.com/topic/128611-solved-how-to-display-all-from-one-table-but-only-if-they-dont-exsist-in-another/#findComment-666581 Share on other sites More sharing options...
esport Posted October 15, 2008 Author Share Posted October 15, 2008 I will give it a go and get back to you, thanks heaps. Quote Link to comment https://forums.phpfreaks.com/topic/128611-solved-how-to-display-all-from-one-table-but-only-if-they-dont-exsist-in-another/#findComment-666586 Share on other sites More sharing options...
esport Posted October 15, 2008 Author Share Posted October 15, 2008 Im not sure if this will make a difference but the coloumn name in the user table is 'id' not 'user_id' but 'id' is a reference in project table. Quote Link to comment https://forums.phpfreaks.com/topic/128611-solved-how-to-display-all-from-one-table-but-only-if-they-dont-exsist-in-another/#findComment-666599 Share on other sites More sharing options...
esport Posted October 15, 2008 Author Share Posted October 15, 2008 this is what i have got so far using an inner query. It doesnt seem to work SELECT e.name, e.surname, e.id as employee_id FROM users u, projects p WHERE u.id=p.employee_id AND u.id NOT IN (SELECT p.employee_id FROM prjects p WHERE p.project_id=$project_id) Quote Link to comment https://forums.phpfreaks.com/topic/128611-solved-how-to-display-all-from-one-table-but-only-if-they-dont-exsist-in-another/#findComment-666605 Share on other sites More sharing options...
Barand Posted October 15, 2008 Share Posted October 15, 2008 Im not sure if this will make a difference but the coloumn name in the user table is 'id' not 'user_id' but 'id' is a reference in project table. Yes, it makes a difference SELECT u.name FROM user u LEFT JOIN project p ON u.id = p.user_id WHERE p.user_id IS NULL Joins are usually faster than subqueries Quote Link to comment https://forums.phpfreaks.com/topic/128611-solved-how-to-display-all-from-one-table-but-only-if-they-dont-exsist-in-another/#findComment-666606 Share on other sites More sharing options...
esport Posted October 15, 2008 Author Share Posted October 15, 2008 Thanks im getting somewhere now. However when I add another clause it doesn't produce any results. This is what I am now using. SELECT u.name, u.surname, u.id, p.project_id FROM users u LEFT JOIN projects p ON u.id=p.user_id WHERE p.project_id=$project_id AND p.user_id IS NULL it doesn't like this p.project_id=$project_id thanks Quote Link to comment https://forums.phpfreaks.com/topic/128611-solved-how-to-display-all-from-one-table-but-only-if-they-dont-exsist-in-another/#findComment-666621 Share on other sites More sharing options...
Barand Posted October 15, 2008 Share Posted October 15, 2008 this is covered in http://www.phpfreaks.com/tutorial/data-joins-unions SELECT u.name, u.surname, u.id, p.project_id FROM users u LEFT JOIN projects p ON u.id=p.user_id AND p.project_id=$project_id WHERE p.user_id IS NULL Quote Link to comment https://forums.phpfreaks.com/topic/128611-solved-how-to-display-all-from-one-table-but-only-if-they-dont-exsist-in-another/#findComment-666624 Share on other sites More sharing options...
esport Posted October 15, 2008 Author Share Posted October 15, 2008 WOW THANKS HEAPS MATE..... :D It works great. Quote Link to comment https://forums.phpfreaks.com/topic/128611-solved-how-to-display-all-from-one-table-but-only-if-they-dont-exsist-in-another/#findComment-666630 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.