0

I want to obtain unread messages count from MySQL database using SELECT SUM operator.

So, my SQL is:

SELECT sum(status_field = 'new') unreadMessagesCount
FROM messages
WHERE 'author_uid' = 'authorUID'

And it returns NULL. Why?

I have items in database and I can select it by (SELECT * FROM messages)

2
  • you have items with status_field = new ? Commented Apr 9, 2021 at 12:28
  • 2
    'author_uid' = 'authorUID' is always false, so no rows will be selected. Commented Apr 9, 2021 at 12:28

1 Answer 1

5

It returns NULL because you are comparing strings, not columns with:

WHERE 'author_uid' = 'authorUID'

And these two strings are not equal. So, all rows are filtered out. The NULL value is because you have an aggregation query. SUM() returns NULL when there are no rows in such a query.

I'm not sure what you intend. Perhaps:

WHERE author_uid = 'authorUID'

However, 'authorUID' seems like a strange value for a uid. You need to put an appropriate value there. If it is a string, enclose it in single quotes. If it is a number, do not use single quotes.

Sign up to request clarification or add additional context in comments.

1 Comment

yes, thanks a lot! You are right, I compared two strings instead of column with specified value!

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.