如果我想使用新的计算列来过滤、聚合和排序数据,任何人都可以告诉哪个查询更有效吗?一种是使用嵌套选择,还是一种在一个选择子句中重复表达式?是否指定了数据库?
查询1:
SELECT count(*), (age - 18) AS newcol FROM qmao_person WHERE (age - 18) > 0 GROUP BY (age - 18) ORDER BY (age - 18) DESC;
查询2:</p>
SELECT count(*), s1.newcol FROM
(SELECT (age - 18) AS newcol FROM qmao_person) s1
WHERE s1.newcol > 0 GROUP BY s1.newcol ORDER BY s1.newcol DESC;
顺便说一句,问题是哪个查询更有效,而不是如何优化查询。