Reputation: 368
I'm stuck with a weird situation. It goes like this:
I have 2 tables. One is called users, which has all my user/password details, then I have another table called userFiles, which has a user ID and a fileID for a file which he has rights to.
Now, note that 1 account can have rights to multiple files, and multiple accounts can have rights to 1 file.
So basically, the first table has, say:
and in table 2, say:
Now, here's what I want to do:
I want to SELECT All the users! (in this case both!), also, I want to JOIN the other table, but I only wanna join the row where I specify a particular fID!
For example, I wanna query for users and user right with fID 67
I wanna get this:
and, for fID 102 (even if it gives a value other than NULL, that i can use in my PHP code, it's fine)
This is the query I've tried using
SELECT * FROM users LEFT JOIN userFiles ON users.ID=userFiles.uID WHERE userFiles.fID = 102
However, this query only returns one row, while I wan't both.
If you can't gimme a mySQL solution, try and help me out so that I can filter out results, and obtain something like what I want using PHP...
Thanks a ton!
Upvotes: 0
Views: 105
Reputation: 5450
You need to move the condition from WHERE to the JOIN, like so:
SELECT * FROM users
LEFT JOIN userFiles ON users.ID=userFiles.uID AND userFiles.fID = 102
Be advised only the users.ID field will be NULL for users who don't have access to the file -- the flID field will still be populated.
For future reference, WHERE conditions filter the entire result set, while the JOIN conditions only filter the records that get joined on the entire result set (without affecting the number of returned rows).
Upvotes: 2