beetle
09-17-2002, 09:52 PM
Ok, here's a snapshot of my datausers
+----+------------+
| id | Type |
+----+------------+
| 1 | Apprentice |
| 2 | LANWizard |
| 3 | Admin |
| 4 | Apprentice |
+----+------------+
todo
+----+-----+--------+
| id | uid | apprID |
+----+-----+--------+
| 1 | 3 | 1 |
| 2 | 2 | 1 |
| 3 | 2 | 0 | <-- Unassigned todo item
+----+-----+--------+Ok, todo.uid is the ID number of the user that the todo item belongs to. todo.apprID is the ID number of the Apprentice that the todo item has been assigned to. I'd like to get the following result from 1 query+--------+--------------+
| apprID | num_of_todos |
+--------+--------------+
| 1 | 2 |
| 4 | 0 |
+--------+--------------+But I can't seem to conjure up the correct syntax to get this done with just 1 query. As you can see I'm using the defualt of 0 for a todo item that hasn't been assigned to an Apprentice. Should this column accept NULL instead?
Follow up:
I've been working on this and here are some results I'm gettingSELECT u.id as apprID, count(apprID) as num_of_todos
FROM users u, todo t
WHERE u.Type = 'Apprentice' and apprID != 0
GROUP BY u.id
result
+--------+--------------+
| apprID | num_of_todos |
+--------+--------------+
| 1 | 2 |
+--------+--------------+
SELECT u.id as apprID, count(apprID) as num_of_todos
FROM users u, todo t
WHERE u.Type = 'Apprentice'
GROUP BY u.id
results
+--------+--------------+
| apprID | num_of_todos |
+--------+--------------+
| 1 | 3 |
| 4 | 3 |
+--------+--------------+
SELECT u.id as apprID, count(apprID) as num_of_todos
FROM users u, todo t
WHERE u.id = t.apprID
GROUP BY u.id
result
+--------+--------------+
| apprID | num_of_todos |
+--------+--------------+
| 1 | 2 |
+--------+--------------+
+----+------------+
| id | Type |
+----+------------+
| 1 | Apprentice |
| 2 | LANWizard |
| 3 | Admin |
| 4 | Apprentice |
+----+------------+
todo
+----+-----+--------+
| id | uid | apprID |
+----+-----+--------+
| 1 | 3 | 1 |
| 2 | 2 | 1 |
| 3 | 2 | 0 | <-- Unassigned todo item
+----+-----+--------+Ok, todo.uid is the ID number of the user that the todo item belongs to. todo.apprID is the ID number of the Apprentice that the todo item has been assigned to. I'd like to get the following result from 1 query+--------+--------------+
| apprID | num_of_todos |
+--------+--------------+
| 1 | 2 |
| 4 | 0 |
+--------+--------------+But I can't seem to conjure up the correct syntax to get this done with just 1 query. As you can see I'm using the defualt of 0 for a todo item that hasn't been assigned to an Apprentice. Should this column accept NULL instead?
Follow up:
I've been working on this and here are some results I'm gettingSELECT u.id as apprID, count(apprID) as num_of_todos
FROM users u, todo t
WHERE u.Type = 'Apprentice' and apprID != 0
GROUP BY u.id
result
+--------+--------------+
| apprID | num_of_todos |
+--------+--------------+
| 1 | 2 |
+--------+--------------+
SELECT u.id as apprID, count(apprID) as num_of_todos
FROM users u, todo t
WHERE u.Type = 'Apprentice'
GROUP BY u.id
results
+--------+--------------+
| apprID | num_of_todos |
+--------+--------------+
| 1 | 3 |
| 4 | 3 |
+--------+--------------+
SELECT u.id as apprID, count(apprID) as num_of_todos
FROM users u, todo t
WHERE u.id = t.apprID
GROUP BY u.id
result
+--------+--------------+
| apprID | num_of_todos |
+--------+--------------+
| 1 | 2 |
+--------+--------------+