king

python 3 mysql sql逻辑查询语句执行顺序

king IIS 2023-02-27 962浏览 0

一 、SELECT语句关键字的定义顺序

SELECT DISTINCT <select_list>
FROM <left_table>
<join_type> JOIN <right_table>
ON <join_condition>
WHERE <where_condition>
GROUP BY <group_by_list>
HAVING <having_condition>
ORDER BY <order_by_condition>
LIMIT <limit_number>

二 、SELECT语句关键字的执行顺序

 1 (7)     SELECT 
 2 (8)     DISTINCT <select_list>
 3 (1)     FROM <left_table>
 4 (3)     <join_type> JOIN <right_table>
 5 (2)     ON <join_condition>
 6 (4)     WHERE <where_condition>
 7 (5)     GROUP BY <group_by_list>
 8 (6)     HAVING <having_condition>
 9 (9)     ORDER BY <order_by_condition>
10 (10)    LIMIT <limit_number>

三、 准备表和数据

1. 新建一个测试数据库TestDB;

create database TestDB;

2.创建测试表table1和table2;

CREATE TABLE table1
 (
     customer_id VARCHAR(10) NOT NULL,
     city VARCHAR(10) NOT NULL,
     PRIMARY KEY(customer_id)
 )ENGINE=INNODB DEFAULT CHARSET=UTF8;

 CREATE TABLE table2
 (
     order_id INT NOT NULL auto_increment,
     customer_id VARCHAR(10),
     PRIMARY KEY(order_id)
 )ENGINE=INNODB DEFAULT CHARSET=UTF8;

View Code

继续浏览有关 数据库技术文章/教程 的文章
发表评论