mysql - Why does changing the comparison in a HAVING clause from '=' to '<' change the output when there are no matches? -


in following query, changing comparison operator in having clause '=' '<' when query returns no results changes output there's either 1 row returned (of nulls) or no rows returned.

can explain why behaviour demonstrated? i'd ideally have first query return 0 rows, nice if done without wrapping in query exclude nulls.

query:

select `templates`.*  `templates`  inner join `items` on `items`.`template_id` = `templates`.`id`  having count(items.id) = 0 

results:

null,null,null... (1 row(s) returned) 

in comparison to:

query:

select `templates`.*  `templates`  inner join `items` on `items`.`template_id` = `templates`.`id`  having count(items.id) < 0 

results:

(0 row(s) returned) 

but also, variation having count(items.id) < 1 returns row of nulls:

query:

select `templates`.*  `templates`  inner join `items` on `items`.`template_id` = `templates`.`id`  having count(items.id) < 1 

results:

null,null,null... (1 row(s) returned) 

simple, count() function never return negative. condition says having count(items.id) < 0 means having 0 < 0 false. having count(items.id)=0 can true means or count(items.id)<1 can true means 0=0 or 0<1 true.

mysql> select 1 test  having count(id)=0; +---+ | 1 | +---+ | 1 | +---+ 1 row in set (0.29 sec)  mysql> select 1 test having count(id)<0; empty set (0.00 sec) 

Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -