我有以下查询:
this->getDoctrine()->getManager();
$sql = "SELECT shop.*
FROM instagram_shop shop
LEFT JOIN instagram_shop_picture picture
ON picture.shop_id = shop.id
WHERE COUNT(picture) = 0
AND shop.isLocked = 0
AND shop.expirydate IS NOT NULL
AND shop.expirydate > now()
AND shop.deletedAt IS NULL
"
想要获取所有包含 0 张图片的商店。但这会返回一个 sql 错误:
General error: 1111 Invalid use of group function
这是为什么?
最佳答案
SELECT shop.id
FROM instagram_shop shop
LEFT JOIN instagram_shop_picture picture ON picture.shop_id = shop.id
WHERE shop.isLocked = 0
AND shop.expirydate IS NOT NULL
AND shop.expirydate > now()
AND shop.deletedAt IS NULL
GROUP BY shop.id
HAVING COUNT(picture.shop_id) = 0
关于mysql - 带计数的一对多关系查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26952534/