WHERE vs HAVING

后端 未结 7 2063
北荒
北荒 2020-11-22 08:20

Why do you need to place columns you create yourself (for example select 1 as \"number\") after HAVING and not WHERE in MySQL?

7条回答
  •  情话喂你
    2020-11-22 08:24

    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 is applied before GROUP BY, HAVING is applied after (and can filter on aggregates).

    In general, you can reference aliases in neither of these clauses, but MySQL allows referencing SELECT level aliases in GROUP BY, ORDER BY and HAVING.

    And are there any downsides instead of doing "WHERE 1" (writing the whole definition instead of a column name)

    If your calculated expression does not contain any aggregates, putting it into the WHERE clause will most probably be more efficient.

提交回复
热议问题