Here is my SQL
SELECT items.name, items.id, items.price,
COUNT(cart_items.itemId) AS quantity
FROM `cart_items`
LEFT JOIN `items`
ON cart_items.itemId = items.id
WHERE cart_items.cartId = '2'
There are no rows in the table cart_items with the cartId of 2. Yet, the resultset still shows one empty row. Why? What’s wrong with my query?
(If I set cart_items.cartId ='1' (there are rows in the table with cartId of 1) then everything returns fine.
Excuse me, I’m quite new to this kind of MySQL.
Because you use COUNT() (an aggregating function). You should move counting items to a subquery.