哪里与有
在 MySQL 中,为什么需要将您自己创建的列(例如 select 1 as "number"
)放置在 HAVING
之后,而不是 WHERE
?
与执行 WHERE 1
(编写整个定义而不是列名)相比,有什么缺点吗?
Why do you need to place columns you create yourself (for example select 1 as "number"
) after HAVING
and not WHERE
in MySQL?
And are there any downsides instead of doing WHERE 1
(writing the whole definition instead of a column name)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
其他关于这个问题的回答都没有说到重点。
假设我们有一个表:
并且有 10 行,id 和值都从 1 到 10:
尝试以下 2 个查询:
您将得到完全相同的结果,您可以看到
HAVING
子句可以在没有GROUP BY 子句。区别如下:
上面的查询将引发错误:Error #1054 - Unknown column 'v' in 'where Clause'
WHERE
子句允许条件使用任何表列,但不能使用别名或聚合功能。HAVING
子句允许条件使用选定的 (!) 列、别名或聚合函数。这是因为
WHERE
子句在select之前过滤数据,而HAVING
子句在select之后过滤结果数据。所以将条件放在
WHERE<如果表中有很多行,/code> 子句会更有效。
尝试
EXPLAIN
查看关键区别:您可以看到
WHERE
或HAVING
使用索引,但行不同。All other answers on this question didn't hit upon the key point.
Assume we have a table:
And have 10 rows with both id and value from 1 to 10:
Try the following 2 queries:
You will get exactly the same results, you can see the
HAVING
clause can work without GROUP BY clause.Here's the difference:
The above query will raise error: Error #1054 - Unknown column 'v' in 'where clause'
WHERE
clause allows a condition to use any table column, but it cannot use aliases or aggregate functions.HAVING
clause allows a condition to use a selected (!) column, alias or an aggregate function.This is because
WHERE
clause filters data before select, butHAVING
clause filters resulting data after select.So put the conditions in
WHERE
clause will be more efficient if you have many many rows in a table.Try
EXPLAIN
to see the key difference:You can see either
WHERE
orHAVING
uses index, but the rows are different.WHERE
在GROUP BY
之前应用,HAVING
在之后应用(并且可以对聚合进行过滤)。一般来说,您不能在这些子句中引用别名,但 MySQL 允许在 GROUP BY、ORDER BY 中引用 SELECT 级别别名 和
拥有
。如果您的计算表达式不包含任何聚合,则将其放入
WHERE
子句很可能是更有效率。WHERE
is applied beforeGROUP BY
,HAVING
is applied after (and can filter on aggregates).In general, you can reference aliases in neither of these clauses, but
MySQL
allows referencingSELECT
level aliases inGROUP BY
,ORDER BY
andHAVING
.If your calculated expression does not contain any aggregates, putting it into the
WHERE
clause will most probably be more efficient.主要区别在于
WHERE
不能用于分组项(例如SUM(number)
),而HAVING
可以。原因是
WHERE
是在分组之前完成的,而HAVING
是在分组之后完成的。The main difference is that
WHERE
cannot be used on grouped item (such asSUM(number)
) whereasHAVING
can.The reason is the
WHERE
is done before the grouping andHAVING
is done after the grouping is done.HAVING
用于过滤GROUP BY
中的聚合。例如,要检查重复的名称:
HAVING
is used to filter on aggregations in yourGROUP BY
.For example, to check for duplicate names:
这两个感觉与第一个相同,因为两者都用于说明过滤数据的条件。尽管在任何情况下我们都可以使用“having”代替“where”,但在某些情况下我们不能使用“where”代替“having”。这是因为在选择查询中,“where”过滤“select”之前的数据,而“having”过滤“select”之后的数据。因此,当我们使用实际上不在数据库中的别名时,“where”无法识别它们,但“having”可以。
例如:让表 Student 包含学生 ID、姓名、生日、地址。假设生日是日期类型。
These 2 will be feel same as first as both are used to say about a condition to filter data. Though we can use ‘having’ in place of ‘where’ in any case, there are instances when we can’t use ‘where’ instead of ‘having’. This is because in a select query, ‘where’ filters data before ‘select’ while ‘having’ filter data after ‘select’. So, when we use alias names that are not actually in the database, ‘where’ can’t identify them but ‘having’ can.
Ex: let the table Student contain student_id,name, birthday,address.Assume birthday is of type date.
并继续,
摘录自:
福塔,本。 “Sams 在 10 分钟内自学 SQL(第 5
版)(萨姆斯自学...)。”。
And continues,
Excerpt From:
Forta, Ben. “Sams Teach Yourself SQL in 10 Minutes (5th
Edition) (Sams Teach Yourself...).”.
having 仅与聚合一起使用,但 where 与非聚合语句一起使用
如果你有where word把它放在聚合之前(group by)
Having is only used with aggregation but where with non aggregation statements
If you have where word put it before aggregation (group by)