Forum OpenACS Q&A: Query return 0 row

Request notifications

Posted by hau wan lin on
Can I know why the below SQL statement return 0 row instead of one row
with null value.

Assume there is no user with id = 3 in the table.

Select user_id, count(*) from users where user_id=3 group by use_id;

I face a problem in this type of query where query return 0 row.
Can anyone teach me how to fix it? Thanks for your help

Posted by Michael A. Cleverly on
The above query is kind of redundant. Presumably user_id is constrained to be unique in the users table, so rather than counting and grouping by user_id (when your where clause constrains us to user_id = 3) you could do:
select count(*) from users where user_id=3
and Postgres will return one row (the value being either 0 or 1). The row only "disappears" when you add the group by clause.