Reputation: 10390
I have a simple query as follows:
SELECT sum( people) as total
FROM event
WHERE event.id = 4;
The event id 4
is not in the table (intentionally) so the query returns NULL
. How would I modify the query to deal with NULL
values?
I am trying to use this with a php script so if the id is non existent the php script should fail.
Upvotes: 0
Views: 65
Reputation: 3093
When you pull it into PHP if the value of your summed column is null then you have a null...
$row = $result->fetch_assoc();
if ($row['total'] === null)
...
Upvotes: 2
Reputation: 3766
You can just do it in PHP. I have my database classes return false if the query did not select any records. You can check that a value was returned from your query statement, if there was, you can count the number of rows and make sure they are greater than 0.
Try this in your query..
select * from (SELECT sum( people) as total FROM event WHERE
event.id = 4) as subsel where subsel.total is not null
That should make it so it returns no rows.
Upvotes: 1