Git Product home page Git Product logo

Comments (2)

yuexin1028 avatar yuexin1028 commented on June 1, 2024 1

在实际执行时,SQL查询的大致流程如下:

  1. FROM子句确定基本的数据来源表。
  2. WHERE子句对从FROM子句中获取的数据进行行级别的过滤。
  3. GROUP BY子句对数据进行分组。
  4. HAVING子句基于每个组应用过滤条件,此时是可以使用SELECT列表中定义的别名的,因为在这个阶段,SQL引擎已经知道了每个组的聚合值。
  5. SELECT子句选择并计算每个组的聚合值或其他列,这里的Sum(item_price * quantity) AS total_price就是这样的一个计算。
  6. ORDER BY子句对结果集进行排序。

因此,在上述SQL语句中,total_price别名在HAVING子句中是合法的,因为SQL引擎会在应用HAVING过滤条件之前计算出这个总价格。

from javaguide.

Mr-Write avatar Mr-Write commented on June 1, 2024

我最近在一家公司实习,在熟悉项目的过程中,看到了很多这样的写法,一开始我也是真的蒙了,以前没讲过,因为我在学习的时候老师也是说的不要在 having 中以未聚合的字段作为条件过滤,后来查资料才发现可以提前在 select 中进行聚合,指定别名在 having 中使用。这两种写法有什么效率上的差别吗,有大佬可以解释一下吗

from javaguide.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.