Home Database Mysql Tutorial Mysql语句-执行顺序_MySQL

Mysql语句-执行顺序_MySQL

May 27, 2016 pm 01:45 PM
mysql statement Execution order

1.这样一个问题,作为一个开发人员需要掌握数据库的哪些东西? 在开发中涉及到数据库,基本上只用到了sql语句,如何写sql以及对其进行优化就比较重要,那些mysql的厚本书籍针对的是DBA,我们只需要学习其中的sql就可以了。

2.既然会写sql是目标,那么怎么才能写好sql.学习下面几点:

1)Mysql的执行顺序,这个是写sql的核心,之前遇到的一些错误就是因为对其不了解;

2)如何进行多表查询,优化,这个是很重要的部分;

3)sql语句的函数,sql提供的函数方便了很多操作;

3.这篇对Mysql语句执行顺序的学习做了总结:

1)Mysql语法顺序,即当sql中存在下面的关键字时,它们要保持这样的顺序:

 

select[distinct]
from
join(如left join)
on
where
group by
having
union
order by
limit
Copy after login

<br/>
Copy after login

2)Mysql执行顺序,即在执行时sql按照下面的顺序进行执行:

from
on
join
where
group by
having
select
distinct
union
order by
Copy after login

3)针对上面的Mysql语法顺序和执行顺序,循序渐进进行学习:

建立如下表格orders:

\

注:下面所有语句符合语法顺序(也不可能不符合,因为会报错^_^),只分析其执行顺序:(join和on属于多表查询,放在最后展示)

语句一:

select a.Customer
from orders a
where a.Customer=&#39;Bush&#39; or a.Customer = &#39;Adams&#39;
Copy after login

分析一:首先是from语句找到表格,然后根据where得到符合条件的记录,最后select出需要的字段,结果如下:

\<br/>语句二groupby:groupby要和聚合函数一起使用

select a.Customer,sum(a.OrderPrice)
from orders a
where a.Customer=&#39;Bush&#39; or a.Customer = &#39;Adams&#39;
group by a.Customer
Copy after login

分析二:在from,where执行后,执行group by,同时也根据group by的字段,执行sum这个聚合函数。这样的话得到的记录对group by的字段来说是不重复的,结果如下:<br/>\<br/>

语句三having:

select a.Customer,sum(a.OrderPrice)
from orders a
where a.Customer=&#39;Bush&#39; or a.Customer = &#39;Adams&#39;
group by a.Customer
having sum(a.OrderPrice) > 2000
Copy after login

分析三:由于where是在group之前执行,那么如何对group by的结果进行筛选,就用到了having,结果如下:

\

语句四distinct: (为测试,先把数据库中Adams那条记录的OrderPrice改为3000)

select distinct sum(a.OrderPrice)
from orders a
where a.Customer=&#39;Bush&#39; or a.Customer = &#39;Adams&#39; or a.Customer = &#39;Carter&#39;
group by a.Customer
having sum(a.OrderPrice) > 1700
Copy after login

分析四:将得到一条记录(没有distinct,将会是两条同样的记录):

\

语句五union:完全是对select的结果进行合并(默认去掉重复的记录):

select distinct sum(a.OrderPrice) As Order1
from orders a
where a.Customer=&#39;Bush&#39; or a.Customer = &#39;Adams&#39; or a.Customer = &#39;Carter&#39;
group by a.Customer
having sum(a.OrderPrice) > 1500
union
select distinct sum(a.OrderPrice) As Order1
from orders a
where a.Customer=&#39;Bush&#39; or a.Customer = &#39;Adams&#39; or a.Customer = &#39;Carter&#39;
group by a.Customer
having sum(a.OrderPrice) > 2000
Copy after login

分析五:默认去掉重复记录(想保留重复记录使用union all),结果如下:

\

语句六order by:

select distinct sum(a.OrderPrice) As order1
from orders a
where a.Customer=&#39;Bush&#39; or a.Customer = &#39;Adams&#39; or a.Customer = &#39;Carter&#39;
group by a.Customer
having sum(a.OrderPrice) > 1500
union
select distinct sum(a.OrderPrice) As order1
from orders a
where a.Customer=&#39;Bush&#39; or a.Customer = &#39;Adams&#39; or a.Customer = &#39;Carter&#39;
group by a.Customer
having sum(a.OrderPrice) > 2000
order by order1
Copy after login

分析:升序排序,结果如下:

\

语句七limit:

select distinct sum(a.OrderPrice) As order1
from orders a
where a.Customer=&#39;Bush&#39; or a.Customer = &#39;Adams&#39; or a.Customer = &#39;Carter&#39;
group by a.Customer
having sum(a.OrderPrice) > 1500
union
select distinct sum(a.OrderPrice) As order1
from orders a
where a.Customer=&#39;Bush&#39; or a.Customer = &#39;Adams&#39; or a.Customer = &#39;Carter&#39;
group by a.Customer
having sum(a.OrderPrice) > 2000
order by order1
limit 1
Copy after login

分析七:取出结果中的前1条记录,结果如下:

\<br/>语句八(上面基本讲完,下面是join 和 on):

select distinct sum(a.OrderPrice) As order1,sum(d.OrderPrice) As order2
from orders a
left join (select c.* from Orders c) d 
on a.O_Id = d.O_Id
where a.Customer=&#39;Bush&#39; or a.Customer = &#39;Adams&#39; or a.Customer = &#39;Carter&#39;
group by a.Customer
having sum(a.OrderPrice) > 1500
union
select distinct sum(a.OrderPrice) As order1,sum(e.OrderPrice) As order2
from orders a
left join (select c.* from Orders c) e 
on a.O_Id = e.O_Id
where a.Customer=&#39;Bush&#39; or a.Customer = &#39;Adams&#39; or a.Customer = &#39;Carter&#39;
group by a.Customer
having sum(a.OrderPrice) > 2000
order by order1
limit 1
Copy after login

分析八:上述语句其实join on就是多连接了一张表,而且是两张一样的表,都是Orders。 执行过程是,在执行from关键字之后根据on指定的条件,把left join指定的表格数据附在from指定的表格后面,然后再执行where字句。

 

注:

1)使用distinct要写在所有要查询字段的前面,后面有几个字段,就代表修饰几个字段,而不是紧随distinct的字段;

2)group by执行后(有聚合函数),group by后面的字段在结果中一定是唯一的,也就不需要针对这个字段用distinct;

 

以上就是Mysql语句-执行顺序_MySQL的内容,更多相关内容请关注PHP中文网(www.php.cn)!


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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 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)

Reduce the use of MySQL memory in Docker Reduce the use of MySQL memory in Docker Mar 04, 2025 pm 03:52 PM

This article explores optimizing MySQL memory usage in Docker. It discusses monitoring techniques (Docker stats, Performance Schema, external tools) and configuration strategies. These include Docker memory limits, swapping, and cgroups, alongside

How to solve the problem of mysql cannot open shared library How to solve the problem of mysql cannot open shared library Mar 04, 2025 pm 04:01 PM

This article addresses MySQL's "unable to open shared library" error. The issue stems from MySQL's inability to locate necessary shared libraries (.so/.dll files). Solutions involve verifying library installation via the system's package m

How do you alter a table in MySQL using the ALTER TABLE statement? How do you alter a table in MySQL using the ALTER TABLE statement? Mar 19, 2025 pm 03:51 PM

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

Run MySQl in Linux (with/without podman container with phpmyadmin) Run MySQl in Linux (with/without podman container with phpmyadmin) Mar 04, 2025 pm 03:54 PM

This article compares installing MySQL on Linux directly versus using Podman containers, with/without phpMyAdmin. It details installation steps for each method, emphasizing Podman's advantages in isolation, portability, and reproducibility, but also

What is SQLite? Comprehensive overview What is SQLite? Comprehensive overview Mar 04, 2025 pm 03:55 PM

This article provides a comprehensive overview of SQLite, a self-contained, serverless relational database. It details SQLite's advantages (simplicity, portability, ease of use) and disadvantages (concurrency limitations, scalability challenges). C

Running multiple MySQL versions on MacOS: A step-by-step guide Running multiple MySQL versions on MacOS: A step-by-step guide Mar 04, 2025 pm 03:49 PM

This guide demonstrates installing and managing multiple MySQL versions on macOS using Homebrew. It emphasizes using Homebrew to isolate installations, preventing conflicts. The article details installation, starting/stopping services, and best pra

How do I configure SSL/TLS encryption for MySQL connections? How do I configure SSL/TLS encryption for MySQL connections? Mar 18, 2025 pm 12:01 PM

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? Mar 21, 2025 pm 06:28 PM

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

See all articles