mu_tou_man

导航

 
查询语句执行顺序:

1、FROM table1 left join table2 on 将table1和table2中的数据产生笛卡尔积,生成Temp1

 

2、JOIN table2 所以先是确定表,再确定关联条件

 

3、ON table1.column = table2.columu 确定表的绑定条件 由Temp1产生中间表Temp2

 

4、WHERE 对中间表Temp2产生的结果进行过滤 产生中间表Temp3

 

5、GROUP BY 对中间表Temp3进行分组,产生中间表Temp4

 

6、HAVING 对分组后的记录进行聚合 产生中间表Temp5

 

7、SELECT 对中间表Temp5进行列筛选,产生中间表 Temp6

 

8、DISTINCT 对中间表 Temp6进行去重,产生中间表 Temp7

 

9、ORDER BY 对Temp7中的数据进行排序,产生中间表Temp8

 

10  LIMIT 对中间表Temp8进行分页,产生中间表Temp9


 例子:

#创库删库,修改库名的语句不存在了
create database if not EXISTS `test_for` ;
drop database if  EXISTS `test_for`; 
create database if not EXISTS `test_01` ;

use `test_01`

#######################################################
#######################################################
#######################################################
#创表
create table `student1`
(
`id` VARCHAR(20),
`name` VARCHAR(20),
`age` VARCHAR(3),
PRIMARY KEY(`id`),
INDEX idx_id(`id`)

);
#删表
DROP TABLE IF EXISTS `student`;
#创表
CREATE table `student`
(
`id` VARCHAR(20) not NULL,
`name` VARCHAR(20) not NULL DEFAULT '',
`age` VARCHAR(3) not NULL  DEFAULT '',
PRIMARY KEY(`id`)
);
#改表名
RENAME table `student` to  `person`;
RENAME table `person` to  `student`;

#######################################################
#######################################################
#######################################################
#修改列数据类型
Alter TABLE `student` MODIFY COLUMN `name` TINYINT;
#修改列长度
Alter TABLE `student` MODIFY COLUMN `name` VARCHAR(20) not null DEFAULT '';
#增列
ALTER TABLE `student` Add COLUMN  `sex` VARCHAR(1) not null;
#删列
ALTER TABLE `student` DROP COLUMN `sex`;
#改列名
AlTER TABLE `student` RENAME COLUMN `name` TO `username`;
AlTER TABLE `student` RENAME COLUMN `username` TO `name`;

ALTER TABLE `student` ADD INDEX idx_id (id);
ALTER TABLE `student` DROP INDEX idx_id;
CREATE UNIQUE INDEX idx_id ON student(id) 
ALTER TABLE `student` DROP INDEX idx_id;
########################################################
########################################################
########################################################
#增数据
INSERT INTO `student` VALUES('1','zhangsan','20');
#增加多条数据
INSERT INTO `student`(`id`,`name`,`age`) VALUES
('2','lisi','21'),
('3','wangwu','25'),
('4','zhanliu','16'),
('5','chenqi','67'),
('6','xuba','58'),
('7','yangjiu','33');

#删除数据
DELETE  FROM `student` where id ='7';
#修改数据
UPDATE student set age='66',`name`='liliu' where id>'4';
UPDATE student set `name`='wangba' where id>'5';
#查询数据并排序
SELECT * from student where age>'18' ORDER BY age DESC;
#查询数据并且分组
SELECT age,count(age) as age_count from student GROUP BY age
SELECT sex,AVG(age) as meanAge from student GROUP BY sex  ORDER BY meanAge
#去重查询(查询学生都考了哪些分数,每个分数出现一次)
SELECT DISTINCT score.s_score FROM score

#UNION(去重)
select id from student UNION  SELECT s_id FROM score where score.s_score>'80'
#UNION ALL(不去重)
select id from student UNION  ALL SELECT s_id FROM score


####################################################
####################################################
####################################################

#inner join 两张表
SELECT a.id,a.name,b.s_id,b.s_score as score from student a JOIN score b on a.id=b.s_id

#inner join 三张表
SELECT a.name,c.c_name ,b.s_score from student a JOIN score b INNER JOIN course c on a.id=b.s_id and c.c_id=b.c_id

#left join(a全显示,a中有而b中没有的,在b中补null)
select a.*,b.* from student a LEFT JOIN score b on a.id=b.s_id
#left join(只显示a中有的,不包含ab的交集部分)
select a.*,b.* from student a LEFT JOIN score b on a.id=b.s_id where b.s_id is NULL

#right join(b全显示,b中有而a中没有的,在a列补null)
select a.*,b.* from student a RIGHT JOIN score b on a.id=b.s_id

#right join(只显示b中的,不包含ab交集部分)
select a.*,b.* from student a RIGHT JOIN score b on a.id=b.s_id where a.id is null

#outer join(mysql不支持outer join,用左右连接做union)
select a.*,b.* from student a LEFT JOIN score b on a.s_id=b.s_id
union
select a.*,b.* from student a RIGHT JOIN score b on a.s_id=b.s_id

#不包含AB交集的集合
select a.*,b.* from student a LEFT JOIN score b on a.s_id=b.s_id where b.s_id is NULL
UNION
select a.*,b.* from student a RIGHT JOIN score b on a.s_id=b.s_id where a.s_id is null

##################################################################
##################################################################
SELECT name FROM student WHERE name like '%U';
SELECT name FROM student WHERE name REGEXP 'U$';
SELECT name FROM student WHERE name REGEXP '^[zw]|U$';
SELECT VERSION();
SELECT USER();

 

 

 

 

  

 

posted on 2022-04-05 20:27  mu_tou_man  阅读(33)  评论(0编辑  收藏  举报