paradoxperfect
02-24-2007, 11:27 PM
How can I ONLY count rows where a certain field is null?
Here's my statement so far:
$query = "SELECT assets.id, assets.ast_serial_num, assets.ast_image_id, assets.ast_history, assets.ast_asset_tag, users.usr_name, COUNT(users.usr_name) FROM ((assets LEFT JOIN rel_ua ON rel_ua.rel_asset_tag = assets.id) LEFT JOIN users ON rel_ua.rel_login_id = users.id AND rel_ua.date_returned is NULL ) GROUP BY assets.ast_asset_tag";
In the table rel_ua it stores the id of the users and assets in relation to how they are assigned along with a returned date
Ex:
usr_id | asset_id | returned_date
1 | 1 | NULL
2 | 1 | NULL
1 | 2 | 2007-01-01
3 | 2 | NULL
1 | 3 | NULL
But if I do the above query it'll count the users assigned to asset 2 and think that it's TWO users... when I just want the ones that have NULL. Like if it was on the asset id 3, it would only display the 1 user...
I want to have a statement where it'd kind of be like COUNT(users.usr_name WHERE rel_ua.returned_date is NULL) but I don't that it's valid...
This is in relation to my other thread... but I've sort of solved some of the problems. If you need more code (like what it's supposed to be outputting etc) let me know. :)
Thanks!
Here's my statement so far:
$query = "SELECT assets.id, assets.ast_serial_num, assets.ast_image_id, assets.ast_history, assets.ast_asset_tag, users.usr_name, COUNT(users.usr_name) FROM ((assets LEFT JOIN rel_ua ON rel_ua.rel_asset_tag = assets.id) LEFT JOIN users ON rel_ua.rel_login_id = users.id AND rel_ua.date_returned is NULL ) GROUP BY assets.ast_asset_tag";
In the table rel_ua it stores the id of the users and assets in relation to how they are assigned along with a returned date
Ex:
usr_id | asset_id | returned_date
1 | 1 | NULL
2 | 1 | NULL
1 | 2 | 2007-01-01
3 | 2 | NULL
1 | 3 | NULL
But if I do the above query it'll count the users assigned to asset 2 and think that it's TWO users... when I just want the ones that have NULL. Like if it was on the asset id 3, it would only display the 1 user...
I want to have a statement where it'd kind of be like COUNT(users.usr_name WHERE rel_ua.returned_date is NULL) but I don't that it's valid...
This is in relation to my other thread... but I've sort of solved some of the problems. If you need more code (like what it's supposed to be outputting etc) let me know. :)
Thanks!