为什么需要将自己创建的列(例如select 1 as "number"
)放在HAVING
之后而不是WHERE
MySQL?
除了WHERE 1
(写整个定义而不是列名)还有什么缺点吗?
最佳答案
Why is it that you need to place columns you create yourself (for example "select 1 as number") after HAVING and not WHERE in MySQL?
WHERE
在 GROUP BY
之前应用,HAVING
在之后应用(并且可以过滤聚合)。
一般来说,你不能在这两个子句中引用别名,但是 MySQL
允许在 GROUP BY
、 中引用
和 SELECT
级别的别名>ORDER BYHAVING
。
And are there any downsides instead of doing "WHERE 1" (writing the whole definition instead of a column name)
如果您的计算表达式不包含任何聚合,将其放入 WHERE
子句很可能会更有效。
关于mysql - 在哪里与拥有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2905292/