1·查找zhao 这个人,告诉我他考试的分数
select scroe from chen where name='zhao';
2·查找王这个人,告诉我他的年纪和分数
select age,scroe from chen where name='wang';
3·not in (不在里面的其他信息)
不显示年纪是20,21岁的所有信息;
select * from chen where age not in (20,21);
4·between 查询范围内的数据
查询年纪在19-23岁范围内的所有信息
select * from stu where age between 19 and 23;
5·查找分数为空的所有信息 is null
select * from stu where scroe is null;
6·查找分数不为空的所有信息is not null
select * from stu where scroe is not null;
7·模糊匹配,开头,结尾,中间
like
select * from yang where age like '6%'; 开头
select * from yang where age like '%7'; 结尾
select * from yang where age like '%4%'; 中间
8·截取某行或者某行到某行limit
查找表中的数据:6-10的数据 limit 5,5;
查找表中的数据:8-25的数据 limit 7,18;
查找表中的数据:第7行的数据 limit 7; 或 limit 6,1;
9·排序
从大到小:降序 order by desc
select * from stu order by scroe desc; 降序,从大到小进行排列
从小到大:升序 order by asc
select * from stu order by scroe asc ;升序,从小到大进行排列
注意:order by 前面不能直接去接where条件表达式
where order by scroe desc; 这样是错误的
where name='wang' order by desc; 这样接就可以。
10·分组(去重复) group by
根据姓名分组:
select * from stu group by name;
班级: 班级表(sdt)
1833 :10人
1832: 5人
1831:4人
统计每个班总人数是多少?
select * from sdt ; 查询所有
班级 总数
1833 10
1832 5
1831 4
select * from sdt group by class;
select count(*) from sheng group by class;
having 相当于where条件表达式,where是不能直接使用聚合函数,having可以接
如果某些情况where无法使用,可以尝试使用having
一般having是结合group by 使用且是放在group by后面
数据库常问的面试题:
如何去重
答:使用distinct 去重,group by 去重
11·备份表结构 like
create table stu1 like stu;
把stu里面的表结构备份到stu1,stu1会被数据库创建出来
只备份结构不备份数据
12·备份表的数据
insert into stu1 select * from stu; 把stu里面的数据全部进行备份到stu1里面
insert into stu1(name,age,class) select name,age,class from stu; 把stu中指定的name,age,class,字段备份到stu1中。
13·备份数据库>
mysqldump -uroot -p123456 dcs68>dcs68.sql
把root用户中对应的dcs68这个数据库备份一份,名称是dcs8.sql
此命令操作是在:linux交互界面进行完成的
14·还原数据库<
mysql -uroot -p123456 xiaoqian<DCS68.SQL把dcs68.sql这个文件中的所有表还原到xiaoqian 这个数据库中
15·mysql中常用的聚合函数(重点)
1.统计 count(*)
select count(*) from stu; 统计表中总行数 6行
select count(*)a from stu; 取别名
select count(*) as s from stu ; 取别名
2.求最大值 max
select max(scroe) from stu; 求最大的分数值
注意:分数最大的值可以找出来,如果取其他值,非第一行,也会直接将分数最大值放在第一行
3.求最小值 min
select min(scroe) from stu; 求最小的分数值
4.求和 sum
select sum(scroe) from stu; 求分数的总和
5.求平均 avg
select avg (scroe) from stu; 求表中的分数平均值
|