数据库语句的简单应用2(实验四)--数据查询
1.单表查询
----单表无条件查询-----------------------------------------------------------------------------------------------------------
select * from department; //查询所有属性列

select sno from sc; //查询部分属性列

select distinct sno from sc; //查询没有重复行数据的属性列

----单表带条件查询--------------------------------------------------------------------------------------------------------
select sname from student where dno='D1'; //比较判断

//确定范围的判断用and或者or或者between and或者not between and
select sname from student where dno in ('d1',"d2"); //确定集合的查询,in后面的括号中是一个集合

select * from sc where score is null; //空值查询

alter table sc modify score int(10); //修改数据类型
select * from sc where score>=60 and score<=100; //多重条件查询

select * from sc where score like 98; //字符精确匹配

select * from sc where score like "9%"; //%表示任意长度,_表示一个字符长度的模糊匹配

----对结果进行排序-------------------------------------------------------------------------------------------------------------
select * from sc order by score desc; //对结果进行降序排序,默认为升序排序,注意此时没有where

2.分组查询
聚集函数:count() //统计元祖的个数或者统计某一列值的个数 sum() //某一列的总和 avg() max() min()
select sno 学号,Max(score) 最高分,min(score) 最低分 from sc group by sno; //出现了别名的用法,并且在分组查询

//单列分组 多列分组(有优先级,就是出现的位置) having语句(选择分组)
select sno from sc
where score>=90
order by sno; //分组查询
3.连接查询
----等值连接与非等值连接---------------------------------
----自然连接----------------------------------------------------------------------------------------------------------
select 目标列列表 from 表1 inner join 表2
on 表1.连接列=表2.连接列
where 查询条件; //显式内连接
select 目标列列表 from 表1,表2
where 表1.连接字段=表2.连接字段 and 查询条件; //隐式内连接
----外连接-----------------------------------------------------------------------------------------------------------------
left join //左外连接
right join //右外连接
full(outer) join //全外连接
----自身连接----------------------------------------------------------------------------------------------------------
起别名的方式

浙公网安备 33010602011771号