基本的DQL语句-单表查询

一、DQL语言

        DQL(Data Query Language 数据查询语言)。用途是查询数据库数据,如SELECT语句。是SQL语句 中最核心、最重要的语句,也是使用频率最高的语句。其中,可以根据表的结构和关系分为单表查询和多 表联查。

注意:所有的查询都会得到一张虚拟的表

二.单表查询

        针对数据库中的一张数据表进行查询,可以通过各 种查询条件和方式去做相关的优化。

1.查询语句语法规则 

SELECT [DISTINCT]
{*|表1.*|[ 表1.字段1 [as 字段别名1]
[, 表1.字段2[as 字段别名2]][, …]]}
FROM 表1 [ as 表别名 ]
[ left|right|inner join 表2 on 表之间
的关系 ]
[ WHERE ]
[ GROUP BY ] 
[ HAVING]
[ ORDER BY]
[ LIMIT {[ 位置偏移量,] 行数 }] ; 
DISTINCT 设定DISTINCT可以去掉重复记录。
AS 表名或者字段名过长时,可以用AS关键字起别名,方便操作。
GROUP BY 按组分类显示查询出的数据。
HAVING GROUP BY分组时依赖的分组条件。
ORDER BY将查询出来的结果集按照一定顺序排序完成。
LIMIT 限制显示查询结果的条数。

2.最简单的查询 

select 123;
select 'abc';
select 1+1;

3. 从表中获取数据

        语法:select 字段名,字段名 from  表名

3.1 全字段查询
-- 全字段查询
select sid,sname,birthday,ssex,classid from student;
select * from student;  -- sql优化  1.不要使用*代替字段名
3.2  部分字段查询
-- 部分字段查询
select sname,ssex from student;

4. 字段名起别名

        表名或者字段名过长时,可以用AS关键字起别名,方便操作。

        语法:SELECT 表别名.字段名1 AS 字段别名1, 表别名.字段名2 AS 字段别名2 FROM 表名 AS 表别名

select sname as '姓名' from student;
select sname as '姓名' , birthday '生日' ,ssex 性别 from student;

5.添加一个字段 

select sname ,'猿究院' 学校 from student;

6. distinct 去重

        语法:SELECT DISTINCT 字段名1, 字段名2... FROM 表名 

注意:所有的字段数据要一致才会去重 

select distinct sname,ssex from student;

7.WHERE条件子句 

        语法:SELECT * FROM 表名 [ WHERE 条件];

注意

• WHERE条件子句不是必须的;

• WHERE子句,可以给查询增加条件;

• 条件:为筛选条件,如不指定则修改该表的所有数据。 

 

- 带条件的查询

-- 【where 子句】
select * from student where sid=5;
select * from student where sid<>5;
select * from student where sid>5;
select * from student where sid between 3 and 6;

-- 查找1班的女同学
select * from student where classid=1 and ssex='女';

面试题!!!
        查询 年龄 大于1990-1-1 的同学

select * from student where birthday < '1990-1-1'; 
7.1 LIKE 关键字 

        语法:SELECT * FROM 表名 WHERE 字段 LIKE 条件;

注意

• 在WHERE子句中,使用LIKE关键字进行模糊查询;

• 与“%”一起使用,表示匹配0或任意多个字符;

• 与“_”一起使用,表示匹配单个字符。  

模糊符号:
% 任意多的任意字符
 _ 一个任意字符

-- 模糊符号
-- % 任意多的任意字符
--  _ 一个任意字符
insert into student(sname) values('杨靓');
insert into student(sname) values('杨文齐'),('小小杨'),('杨帅哥'),('帅气的杨同学');

select * from student where sname like '%杨%';
select * from student where sname like '%杨';
select * from student where sname like '杨%';

select * from student where sname like '杨_';
select * from student where sname like '杨__';
select * from student where sname like '杨___';
7.2 IN 关键字 

        语法:SELECT * FROM 表名 WHERE 字段 IN (值1,值2...);

注意

• 查询的字段的值,至少与IN 后的括号中的一个值相同;

• 多个值之间用英文逗号隔开。 

-- in 在某个特定的范围
-- 3 5 7 9
-- or 会让索引失效
select * from student where sid=3 or sid=5 or sid=7 or sid=9;

-- 推荐
-- in 可以使用到索引
7.3 NULL 值查询

        语法:SELECT * FROM 表名 WHERE 字段 IS NULL | IS NOT NULL

 注意

• NULL代表“无值”;

• 区别于零值0和空符串;

• 只能出现在定义允许为NULL的字段;

• 须使用 IS NULL 或 IS NOT NULL 比较操作符去比较。

-- null
-- is 是一个什么
select * from student where birthday is null;
select * from student where birthday is not null;

8.常用的聚合函数 (非常非常重要)

函数名 返回值
AVG(col) 返回指定列的平均值
COUNT(col) 返回指定列中非NULL值的个数
MIN(col)返回指定列的最小值
MAX(col)返回指定列的最大值
SUM(col) 返回指定列的所有值之和

聚合函数(非常重要*****)
把多个值变成一个值

  • count() 统计个数
  • max() 求最大值
  • min()  求最小值
  • sum()  求和
  • avg()  求平均
-- 聚合函数(非常重要*****)
-- 把多个值变成一个值
-- count() 统计个数
-- max() 求最大值
-- min()  求最小值
-- sum()  求和
-- avg()  求平均

-- count() 统计个数
-- count的特征: 任何类型都可以 但是不统计null
-- select count(字段\常量\*) from student;
select count(sid) from student;  -- 主键
select count(classid) from student; -- 不统计null

select count('a') from student;  -- 不推荐
select count(123) from student;  -- 推荐
select count(*) from student;    -- 推荐

-- sum avg min max 数值类型
select sum(score) from sc;
select avg(score) from sc;
select max(score) from sc;
select min(score) from sc;


-- 统计出成绩表中一共有多少次考试,总成绩,平均分,最高分,最低分
/*
SELECT  
    COUNT(DISTINCT sid) AS total_exams,   
    SUM(score) AS total_score,  
    AVG(score) AS average_score, 
    MAX(score) AS highest_score,  
    MIN(score) AS lowest_score 
from sc; 
*/
select count(*) ,sum(score),avg(score),max(score),min(score) from sc;

9.分组 group by  

分组 group by

  • 对所有的数据进行分组统计;
  • 分组的依据字段可以有多个,并 依次分组。
  • HAVING 与GROUP BY结合使用,进行分组 后的数据筛选。
-- 分组 group by ******
--男女同学各有多少人
select ssex,count(1) from student group by ssex;

-- 统计出各班有多少人
select classid,count(1) from student group by classid;

-- 统计成绩表中 每个同学的总分和平均分
select sid,sum(score),avg(score) from sc group by sid;


-- 查询出平均分不及格的学生 sid 平均分
-- having where的区别 (面试)
-- having 对分组聚合后的数据进行筛选
select sid,sum(score),avg(score) from sc  group by sid having avg(score) <60 ;
select sid,sum(score),avg(score) from sc where score<60  group by sid having avg(score) <60 ;

having where的区别 (面试) 

10.ORDER BY排序

        语法:SELECT * FROM 表名 ORDER BY 字段名 [DESC|ASC 

注意

• ORDER BY 表示对SELECT语句查询得到的结果,按字段名进行排序;

• DESC表示排序的顺序为降序,ASC表示排序的顺序为升序;

•“[ ]”包含的内容可以省略。 

-- order by 排序
-- 先写先排
-- 升序 asc 不写(默认)
-- 降序 desc  必须声明
select * from student order by classid desc;  -- 降序
select * from student order by classid asc;  -- 升序

select * from sc order by score desc, cid asc;
select * from sc order by score desc, cid desc;

11.LIMIT关键字 

        语法:SELECT * FROM 表名 LIMIT [n , m ]

注意

• LIMIT关键字是MySQL特有关键字;

• LIMIT限制SELECT返回结果的行数;

• n 表示第一条记录的偏移量,m 表示显示记录的数量;

•“[ ]”包含的内容可以省略 

 limit 分页
步长 从0 开始  (页码-1)*步长,步长
语法:select * from student limit 位置,步长;

-- limit 分页
-- 步长 从0 开始  (页码-1)*步长,步长
select * from student limit 位置,步长;
select * from student limit 0,3;
select * from student limit 3,3;
select * from student limit 6,3;
-- 应用层解决
-- select * from student limit (3-1)*3,3;  -- 错误的

-- 找到成绩及格的总分数排名第二的 sid  总成绩
SELECT sid,sum(score) from sc  where score >=60 group by sid order by sum(score) desc LIMIT 1,1;
  
   

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值