To do this you need an outer join. By the way, the way you are writing your query with an implicit join is outdated and no longer recommended. Using the JOIN keyword is recommended. This also makes it easier to change an inner join to an outer join.
FROM categories AS sc
LEFT JOIN products AS s
ON s.ownerid=sc.id
To return 0 instead of NULL use IFNULL(..., 0)
. The entire query becomes:
SELECT
sc.*,
IFNULL(MIN(s.price), 0) AS minp,
IFNULL(MAX(s.price), 0) AS maxp
FROM categories AS sc
LEFT JOIN products AS s
ON s.ownerid = sc.id
GROUP BY sc.id
You may also want to consider if it would be better to return the default NULL instead of 0 for categories that have no products.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…