0

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.

2 Answers 2

2

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)
    ...
Sign up to request clarification or add additional context in comments.

1 Comment

let me try your solution
1

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.

2 Comments

if it returns null it still counts as a row so that wont work
mysqli. What you are doing is returning an empty set in my problem it still returns a row with the value null

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.