Put very simply I have two tables:
- users
- briefs
I am trying to SELECT several fields from the 'users' table but within the WHERE clause the 'users.userID = briefs.userID'.
Here's my problem; that users may have a row in the users table but not necessarily in the briefs table. So if there is no corresponding row to be found where the two ID's match I obviously get no row returned.
So how do I select all users with AND without an entry in the briefs table (or for example displaying '0' for those without an entry in the briefs table) from a query?
Currently I have:
---
SELECT users.userID, users.prefix, users.firstname, users.surname, users.lastlogin, COUNT(briefs.briefID) AS briefs
FROM users, briefs
WHERE (users.userID = briefs.userID)
GROUP BY briefs.userID
ORDER BY users.surname
---
So I realise the WHERE clause is causing those rows without a corresponding entry in the BRIEFS table to not be displayed. So my question is how to return all those values currently being returned with the SQL above; plus displaying '0' (zero) for those without a corresponding row in the briefs table.
I've been looking at this and thinking I may need to use a CASE or IF function; but thought there may be a simpler way I'm just missing!
Many thanks,
Lewis.
- users
- briefs
I am trying to SELECT several fields from the 'users' table but within the WHERE clause the 'users.userID = briefs.userID'.
Here's my problem; that users may have a row in the users table but not necessarily in the briefs table. So if there is no corresponding row to be found where the two ID's match I obviously get no row returned.
So how do I select all users with AND without an entry in the briefs table (or for example displaying '0' for those without an entry in the briefs table) from a query?
Currently I have:
---
SELECT users.userID, users.prefix, users.firstname, users.surname, users.lastlogin, COUNT(briefs.briefID) AS briefs
FROM users, briefs
WHERE (users.userID = briefs.userID)
GROUP BY briefs.userID
ORDER BY users.surname
---
So I realise the WHERE clause is causing those rows without a corresponding entry in the BRIEFS table to not be displayed. So my question is how to return all those values currently being returned with the SQL above; plus displaying '0' (zero) for those without a corresponding row in the briefs table.
I've been looking at this and thinking I may need to use a CASE or IF function; but thought there may be a simpler way I'm just missing!
Many thanks,
Lewis.