找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手
1、查找‘zhao'这个人,告知我他的考试分数
eg:select scroe from stu where name='zhao';

2、查找wang这个人,告诉我他年纪个分数
eg:select age ,scroe from stu where name='wang';

3、not in (不在里面的其他信息)
不显示年纪是20,21岁的所有信息;
eg:select * from stu where age not in(20,21);

4、between 查询范围内的数据
查询年纪19-23范围内的所有信息
eg:select * from stu where age between 19 and 23;

5、查找分数为空的所有信息 is null
eg:select * from stu where scroe is null;

6、查找分数不为空的所有信息 is not null
eg:select * from stu where scroe is not null;

7、like 模糊匹配开头、结尾、中间
select * from stu where name like'zh%'; 只能联想到开头
select * from stu where name like'%ao'; 只能联想到结尾
select * from stu where name like'%a%'; 只能联想到中间

8、截取某行或者某行到某行的数据 limit
查找表中的数据:6-10的数据  6-1=5  5+?=10  5
  select * from stu  limit 5,5;
查找表中的数据:8-25行的数据 8-1=7  7+?=25  18
               limit 7,18;
查找表中的数据:7行的数据  7-1=6  6+?=7
               limit 6,1;
如果输入limit 6 就会显示1-6行的数据
前面一个减一,后面一个数用减一后的加多少等于指定行数

9、排序
从小到大 :升序 order by asc
select * from stu  order by  scroe asc;升序 分数从小到大排序
从大到小 :降序 order by desc
select * from stu  order by  scroe desc;降序 分数从大到小排序
注意:order by 前面不能直接接where 条件表达式
where order by scroe desc;错误,不可以这样接
where name='zhang' order by scroe desc;这样可以接

10、分组(去重)
根据姓名分组:select * from stu group by name;
班级     班级表
1833  :10个人  12345678910
1832 :5个人  12345
1831 :4个人  1234
单纯总统计:19人
统计一下每个班总人数是多少?
select * from stu;查询所有
班级       总数
1833        10
1832         5
1831         4
select * from stu 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
只备份结构不备份数据

12、备份表的数据
insert into stu1 select * from stu;把stu表的所有数据进行备份到stu1中
insert into stu1(name,age)select name,age from stu;把stu表中的某几个字段备份到stu1中

13、备份数据库 >
mysqldump -uroot -p123456 dcs68>dcs68.sql
把root用户中对应的dcs68这个数据库备份一份,名称是dcs68.sql
此命令操作是在liunx交互界面完成

14、还原数据库 <
mysql -uroot -p123456 xiaoqian<dcs68.sql
把dcs68.sql这个文件中的所有表还原到xiaoqian这个数据库中。
此命令操作是在liunx交互界面完成

15、mysql中常用的聚合函数(重点)
①、统计 count(*)
select count(*) from stu; #统计表中总行数
select count(*)a from stu;取别名
select count(*)as from stu;取别名
  count(*)=6
  where 7>6 不可以这样写
  where 7>count(*)  也不可以这样写
  where 7>c   要把count起别名为c才可以用
  count(*)+1=7
  当我们统计行数之后,如果需要用行数做事,值不能直接取,只能取值的别名
②、求最大 max
select max(score)from stu;求分数最大的
注意:查出来的分数最大的值肯定是没问题的,但是如果还想取其他值,非第一行,其他都是错误的
③、求最小的 min
select min(score)from stu;求分数最小的
注意:查出来的分数最小的值肯定是没问题的,但是如果还想取其他值,非第一行,其他都是错误的
④、求和 sum
select sum(score)from stu;求分数之和
⑤、求平均 avg
select avg(score)from stu;求分数平均值

分享至 : QQ空间
收藏

0 个回复

您需要登录后才可以回帖 登录 | 立即注册