虽然我可以在别名子查询上进行分组和排序,但我不能在where子句中使用该别名。我需要使用连接吗?
可行:
SELECT entries.*,
(SELECT avg(value)
FROM `ratings`
WHERE ratings.entry_id = entries.id) as avg_rating
FROM `entries`
ORDER BY avg_rating DESC
失败 ("where子句中未知的列'avg_rating'"):
SELECT entries.*,
(SELECT avg(value)
FROM `ratings`
WHERE ratings.entry_id = entries.id) as avg_rating
FROM `entries`
WHERE avg_rating < '4.5000' ORDER BY avg_rating DESC