Home Database Mysql Tutorial Group By 和 Having, Where ,Order by语句的执行顺序

Group By 和 Having, Where ,Order by语句的执行顺序

Jun 07, 2016 pm 04:14 PM
order where implement statement

一个SQL语句往往会产生多个临时视图,那么这些关键字的执行顺序就非常重要了,因为你必须了解这个关键字是在对应视图形成前的字段进行操作还是对形成的临时视图进行操作,这个问题在使用了别名的视图尤其重要。 Group By 和 Having, Where ,Order by这些关键

一个SQL语句往往会产生多个临时视图,那么这些关键字的执行顺序就非常重要了,因为你必须了解这个关键字是在对应视图形成前的字段进行操作还是对形成的临时视图进行操作,这个问题在使用了别名的视图尤其重要。

Group By 和 Having, Where ,Order by这些关键字是按照如下顺序进行执行的:Where, Group By, Having, Order by。

  • 首先where将最原始记录中不满足条件的记录删除(所以应该在where语句中尽量的将不符合条件的记录筛选掉,这样可以减少分组的次数)
  • 然后通过Group By关键字后面指定的分组条件将筛选得到的视图进行分组
  • 接着系统根据Having关键字后面指定的筛选条件,将分组视图后不满足条件的记录筛选掉
  • 最后按照Order By语句对视图进行排序,这样最终的结果就产生了。

在这四个关键字中,只有在Order By语句中才可以使用最终视图的列名,如:

1

2

3

4

5

<code>SELECT FruitName, ProductPlace, Price, ID AS IDE, Discount

FROM T_TEST_FRUITINFO

WHERE (ProductPlace = N'china')

ORDER BY IDE

</code>

Copy after login

这里只有在ORDER BY语句中才可以使用IDE,其他条件语句中如果需要引用列名则只能使用ID,而不能使用IDE。

PS:

  • 凡是在group by后面出现的字段,必须同时在select后面出现;

  • 凡是在select后面出现的、同时未在聚合函数中出现的字段,必须同时出现在group by后面.

  • having 子句被限制子已经在SELECT语句中定义的列和聚合表达式上。

(文:程序员之家)
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Python script to be executed every 5 minutes Python script to be executed every 5 minutes Sep 10, 2023 pm 03:33 PM

Automation and task scheduling play a vital role in streamlining repetitive tasks in software development. Imagine there is a Python script that needs to be executed every 5 minutes, such as getting data from an API, performing data processing, or sending periodic updates. Running scripts manually so frequently can be time-consuming and error-prone. This is where task scheduling comes in. In this blog post, we will explore how to schedule a Python script to execute every 5 minutes, ensuring it runs automatically without manual intervention. We will discuss different methods and libraries that can be used to achieve this goal, allowing you to automate tasks efficiently. An easy way to run a Python script every 5 minutes using the time.sleep() function is to utilize tim

How to use Python for scripting and execution in Linux How to use Python for scripting and execution in Linux Oct 05, 2023 am 11:45 AM

How to use Python to write and execute scripts in Linux In the Linux operating system, we can use Python to write and execute various scripts. Python is a concise and powerful programming language that provides a wealth of libraries and tools to make scripting easier and more efficient. Below we will introduce the basic steps of how to use Python for script writing and execution in Linux, and provide some specific code examples to help you better understand and use it. Install Python

How to perform Brown-Forsythe test in Python How to perform Brown-Forsythe test in Python Aug 31, 2023 pm 11:53 PM

The Brown-Forsythe test is a statistical test used to determine whether the variances of two or more groups are equal. Levene's test uses the absolute deviation from the mean, while the Brown-Forsythe test uses the deviation from the median. The null hypothesis used in the test is as follows - H0: The variances of the groups (population) are equal. The alternative hypothesis is that the variances of the groups (population) are not equal. - H1: The variances of the groups (population) are not equal. To perform the test, we calculate the median of each group and its correlation The absolute deviation of the number of digits. We then calculate the F-statistic based on the variance of these deviations. Assume that the calculated F statistic is greater than the critical value in the F distribution table. In this case, we reject the null hypothesis and conclude that the variances of the groups are not equal. In Python, sc

A Practical Guide to the Where Method in Laravel Collections A Practical Guide to the Where Method in Laravel Collections Mar 10, 2024 pm 04:36 PM

Practical Guide to Where Method in Laravel Collections During the development of the Laravel framework, collections are a very useful data structure that provide rich methods to manipulate data. Among them, the Where method is a commonly used filtering method that can filter elements in a collection based on specified conditions. This article will introduce the use of the Where method in Laravel collections and demonstrate its usage through specific code examples. 1. Basic usage of Where method

How to write PHP code in the browser and keep the code from being executed? How to write PHP code in the browser and keep the code from being executed? Mar 10, 2024 pm 02:27 PM

How to write PHP code in the browser and keep the code from being executed? With the popularization of the Internet, more and more people have begun to come into contact with web development, and learning PHP has also attracted more and more attention. PHP is a scripting language that runs on the server side and is often used to write dynamic web pages. However, during the exercise phase, we want to be able to write PHP code in the browser and see the results, but we don't want the code to be executed. So, how to write PHP code in the browser and keep it from being executed? This will be described in detail below. first,

How to use the Where method in Laravel collections How to use the Where method in Laravel collections Mar 10, 2024 pm 10:21 PM

How to use the Where method in Laravel collection Laravel is a popular PHP framework that provides a wealth of functions and tools to facilitate developers to quickly build applications. Among them, Collection is a very practical and powerful data structure in Laravel. Developers can use collections to perform various operations on data, such as filtering, mapping, sorting, etc. In collections, the Where method is a commonly used method for filtering the collection based on specified conditions.

How to perform ANCOVA in Python? How to perform ANCOVA in Python? Sep 01, 2023 pm 05:21 PM

ANCOVA (analysis of covariance) is a useful statistical method because it allows the inclusion of covariates in the analysis, which can help adjust for auxiliary variables and increase the precision of comparisons between groups. These additional factors or covariates can be included in the study by using ANCOVA. To ensure that observed differences between groups are caused by the treatment or intervention in the study and not by extraneous factors, ANCOVA can be used to adjust for the effect of covariates on the group means. This allows for more accurate comparisons between groups and gives more reliable conclusions about the relationships between variables. In this article, we will take a closer look at ANCOVA and implement it in Python. What is ANCOVA? Analysis of covariance (ANCOVA) method compares two or more groups

From beginner to proficient: Master the skills of using is and where selectors From beginner to proficient: Master the skills of using is and where selectors Sep 08, 2023 am 09:15 AM

From beginner to proficient: Master the skills of using is and where selectors Introduction: In the process of data processing and analysis, the selector is a very important tool. Through selectors, we can extract the required data from the data set according to specific conditions. This article will introduce the usage skills of is and where selectors to help readers quickly master the powerful functions of these two selectors. 1. Use of the is selector The is selector is a basic selector that allows us to select the data set based on given conditions.

See all articles