Skip to content
Advertisement

MySQL COUNT and IFNULL

I have tables like this: SQLFIDDLE

I’m trying to count how many products are in each category

it is almost working except IFNULL part.
I recieve:

but i would also like to get null results:

thanks!

Advertisement

Answer

  1. NATURAL JOIN == NATURAL INNER JOIN whereas you need OUTER JOIN.
  2. Base table is categories, so it must be mentioned first (NATURAL LEFT JOIN takes the values for common columns from the most left table).
  3. You must count definite column, not common column or total rows amount (they will give 1, not 0).
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement