mysql 子查询空_mysql的子查询

2-1

2.1查看表结构与修改表名

########## modify the table name ##########

alter table tb_emp rename jd_emp;

########## show tables in this database ##########

show tables;

########## describe the table ##########

describe jd_emp;

2.2修改字段名与字段数据类型

########## change the column name ##########

alter table tb_emp change Id prod_id int;

########## change the data type of column ##########

alter table tb_emp modify Name varchar(30);

######### End ##########

DESCRIBE tb_emp;

2.3添加与删除字段

########## add the column ##########

alter table tb_emp add Country varchar(20) after Name;

########## delete the column ##########

alter table tb_emp drop Salary;

########## End ##########

DESCRIBE tb_emp;

2.4修改字段的排列位置

########## modify the column to top ##########

alter table tb_emp modify Name varchar(25) first;

########## modify the column to the rear of another column ##########

alter table tb_emp modify DeptId int after Salary;

########## End ##########

DESCRIBE tb_emp;

2.5删除表的外键约束

########## delete the foreign key ##########

alter TABLE tb_emp drop foreign key emp_dept;

########## End ##########

SHOW CREATE TABLE tb_emp \G;

2-2

2-2.1插入数据

########## bundle insert the value ##########

insert into tb_emp(Id,Name,DeptId,Salary)

values(1,'Nancy',301,2300),

(2,'Tod',303,5600),

(3,'Carly',301,3200);

########## End ##########

SELECT * FROM tb_emp;

2-2.2更新数据

########## update the value ##########

update tb_emp set Name="Tracy", DeptId=302,Salary=4300.00 where Id=3;

########## End ##########

SELECT * FROM tb_emp;

2-2.3删除数据

########## delete the value ##########

delete from tb_emp where Salary>3000;

######### End ##########

SELECT * FROM tb_emp;

3- -

3-1.2带IN关键子的查询

使用in关键字检索数据表中指定的数据内容

SELECT 字段名 FROM 表名 WHERE 字段名 NOT IN (n1,n2,n3,...); 。

select Name,Salary from tb_emp where Id not in(1);

3-1.2Between and查询

select Name,Salary from tb_emp where Salary BETWEEN 3000 and 5000;

3-2.1带 LIKE 的字符匹配查询

和LIKE一起搭配使用的就是通配符%和_

select Name,Salary from tb_emp where Name like 'C%';

3-2.2查询空值与去除重复结果

关键字IS NULL检索数据表中指定的字段的空值;使用关键字DISTINCT检索数据表中指定的不重复的内容

select * from tb_emp where DeptId is null;

select distinct Name from tb_emp where Name is not null;

3-2.3带 AND 与 OR 的多条件查询

使用关键字AND检索数据表中指定的字段的内容;使用关键字IN检索数据表中指定的字段的内容

OR 可以和 AND 一起使用。但是 AND 的优先级要高于 OR 的优先级!IN和OR功能相同(多用IN)

select * from tb_emp where DeptId='301' and Salary>3000;

select * from tb_emp where DeptId in('301','303');

3-3.1对查询结果进行排序

如果我们需要对读取的语句进行排序,我们就可以使用Order By子句来设定你想要按照的字段进行排序并返回结果。

SELECT 字段名 FROM 表名 ORDER BY 字段名 [ASC[DESC]];

########## 查询1班同学的所有信息以成绩降序的方式显示结果 ##########

select * from tb_score where class_id='1' order by score desc;

3-3-2分组查询

关键字group by 查询的结构是每个分组中首次出现的一条记录,一般和聚合函数一起使用

########## 对班级名称进行分组查询 ##########

select * from tb_class group by class_id;

3-3.3使用 LIMIT 限制查询结果的数量

SELECT 字段名 FROM 表名 LIMIT [OFFSET,] 记录数;

第一个参数,OFFSET,可选参数,表示偏移量,如果不指定默认值为0,表示从查询结果的第一条记录开始,若偏移量为1,则从查询结果中的第二条记录开始,以此类推。第二个参数,记录数,表示返回查询结果的条数

SELECT 字段名 FROM 表名 LIMIT [OFFSET,] 记录数;。

########## 查询班级中第2名到第5名的学生信息 ##########

select * from tb_score where score order by score desc limit 1,4;

4-1

4.1COUNT( )函数

########## 查询该表中一共有多少条数据 ##########

select count() from tb_class;

########## 查询此表中367班有多少位学生 ##########

select classid,count() from tb_class where classid=367;

4.2SUM()函数

########## 查询所有学生总分数 ##########

select sum(score) from tb_class;

select course,sum(score) from tb_class where course='语文';

########## 查询学生语文科目的总分数 ##########

4.3AVG( )函数

########## 查询学生语文科目的平均分数 ##########

select course,avg(score) from tb_class where course ='语文';

select course,avg(score) from tb_class where course ='英语';

########## 查询学生英语科目的平均分数 ##########

4.4MAX()函数

########## 查询学生语文科目的平均分数 ##########

select course,avg(score) from tb_class where course ='语文';

select course,avg(score) from tb_class where course ='英语';

########## 查询学生英语科目的平均分数 ##########

4.5MIN()函数

########## 查询语文课程中的最低分数 ##########

select course,min(score) from tb_class where course='语文';

select course,min(score) from tb_class where course='英语';

########## 查询英语课程中的最低分数 ##########

4-2

4-2.1内连接查询

select tb_student.name as studentName,tb_class.name as className from tb_student join tb_class on tb_student.class_id = tb_class.id;

4-2.2外连接查询

########## 使用左外连接查询所有学生姓名和对应的班级

select tb_student.name as studentName,tb_class.name as className from tb_student left join tb_class on tb_student.class_id = tb_class.id;

########## 使用右外连接查询所有学生姓名和对应的班级 ##########

select tb_student.name as studentName,tb_class.name as className from tb_student right join tb_class on tb_student.class_id = tb_class.id;

4-2.3复合条件查询

########## 查询所有班级里分数在90分以上的学生的姓名和学生的成绩以及学生所在的班级 ##########

select tb_student.name as studentName,tb_student.score,tb_class.name as className from tb_student join tb_class on tb_student.class_id = tb_class.id where tb_student.score>90 order by score desc;

子查询允许把一个查询嵌套在另一个查询当中。子查询,又叫内部查询,相对于内部查询,包含内部查询的就称为外部查询。

子查询可以包含普通 select可以包括的任何子句,比如: distinct、 group by、 order by、 limit 、 join 等;但是对应的外部查询必须是以下语句之一: select、 insert、 update或者 delete。

子查询的位置: select 中、 from 后、 where 中 group by和 order by中无实用意义。

1.带有比较运算符的子查询是指父查询和子查询使用比较运算符连接的嵌套查询;

使用场景:当用户能够确切的知道内层查询返回的是单个值时,可以使用比较运算符。

select id,name, dept_id from employee where dept_id=(select dept_id from employee where name='Tom');

select name,age from tb_emp where age>(select avg(age) from tb_emp);

2.关键字查询

列子查询返回的结果集是 N 行一列,因此不能直接使用 = 、>、=、<=、<>这些比较标量结果的操作符。

在列子查询中可以使用 ALL、ANY、SOME 和 IN关键字操作符。

ALL必须接在一个比较运算符的后面,表示与子查询返回的所有值比较都为 TRUE则返回TRUE

1.使用 ALL 关键字进行查询

select position,salary from tb_salary where salary>all(select max(salary) from tb_salary where position='Java');

2.使用 ANY 关键字进行查询

select position,salary from tb_salary where salary>ANY(select min(salary) from tb_salary where position='Java');

3.使用 IN 关键字进行查询

select position,salary from tb_salary where position='Java';

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值