找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手
讲数据库
1、查找zhao这个人,告诉我他考试的分数
select scroe from stu where name='zhao';
2、查找wang这个人,告诉我他年纪和分数;
select age,scroe from stu where name='wang';
3、not in(不在里面的其它信息)
不显示年纪是20,21岁的所有信息;
select * from stu 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 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
          limit 5,5;
查找表中的数据:8-25的数据  8-1=7   7+?=25 18
                  limit 7,18;
查找表中的数据:第7行的数据 7-1=6    6+?=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 asc;错误不能这样接
where name='zhangsan' order by scroe asd;这样可以接
10、分组(去重)
根据姓名分组:
select * from stu group by name;
班级:班级表(sdt)
1833 :10个人 12345678910
1832 :5个人  12345
1831 :4个人  1234
单纯总统计:19
统计一下每个班总人数是多少?
select * from sdt;查询所有
班级 总数
1833  10  
1832  5  
1831  4
select * from std 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这个数据库中
15、MySQL中常用的聚合函数(重点)
1、统计 count(*)
select count(*)from stu; #统计表中总行数
select count(*)a from stu;取别名
select count(*)as a from stu;取别名
2、求最大 max
select max(score)from stu;求分数最大的
注意:分数最大的值肯定是OK,但是如果你取其它值,非第一行,其它都是
错误的。
3、求最小 min
select min(score)from stu;求分数最小的
注意:分数最小的值肯定是OK,但是如果你取其它值,非第一行,其它都是
错误的。
4、求和 sum
select sum(score) from stu;求分数之和
5、求平均 avg
select avg(scroe)from stu;求分数平均值
F:\有道\qq6BEB6B353C8A71D5BFEED469BAEDE378\9c9d1e2b3d704fb9b9b2f629957a394c\1.png

F:\有道\qq6BEB6B353C8A71D5BFEED469BAEDE378\b8b76bb282a042a588b2a2dd4d356e3d\2.png

F:\有道\qq6BEB6B353C8A71D5BFEED469BAEDE378\abf9f0d736034182aecbe512f6a5ae62\3.png

F:\有道\qq6BEB6B353C8A71D5BFEED469BAEDE378\bf77edaae6e647f8a46887f2fd5faf0d\4.png

F:\有道\qq6BEB6B353C8A71D5BFEED469BAEDE378\7cd8463457054a38aae6cbc8e38b3ac1\5.png

F:\有道\qq6BEB6B353C8A71D5BFEED469BAEDE378\28db18acd824417a9f6c67510005f7a3\6.png

F:\有道\qq6BEB6B353C8A71D5BFEED469BAEDE378\0806866e7bd2471b856b925f84d926f9\7.png

F:\有道\qq6BEB6B353C8A71D5BFEED469BAEDE378\18bab9b07b1e4a7da5b1d6d36c1d62f5\8.png

F:\有道\qq6BEB6B353C8A71D5BFEED469BAEDE378\9b703a4cf4344e6cb8bee12632c50934\9.png

F:\有道\qq6BEB6B353C8A71D5BFEED469BAEDE378\b95a903aecef440aba2c47a6e3dccab1\10.png

F:\有道\qq6BEB6B353C8A71D5BFEED469BAEDE378\1de930e434664dbab6c901b7107341a5\11.png

F:\有道\qq6BEB6B353C8A71D5BFEED469BAEDE378\071b7c86bb59497ca098a08224f3b573\12.png

F:\有道\qq6BEB6B353C8A71D5BFEED469BAEDE378\7c68044d96e546068f77e44b5ffb8084\13.png

F:\有道\qq6BEB6B353C8A71D5BFEED469BAEDE378\fecc1dda9bf548c5b0f78bfadf27c1a0\14.png

F:\有道\qq6BEB6B353C8A71D5BFEED469BAEDE378\49053421382340f2ad0c71281f3406d0\15.png

F:\有道\qq6BEB6B353C8A71D5BFEED469BAEDE378\4edd81ca995c4eb691d7a02a23d21449\16.png

F:\有道\qq6BEB6B353C8A71D5BFEED469BAEDE378\6d3d4cc1e75e4238a979f2058d1191ce\17.png

F:\有道\qq6BEB6B353C8A71D5BFEED469BAEDE378\df4c7e5e5af64cd1bdf9be2e9bc42533\18.png

F:\有道\qq6BEB6B353C8A71D5BFEED469BAEDE378\9e06d6dcdb2441f4809529730fee925d\19.png

F:\有道\qq6BEB6B353C8A71D5BFEED469BAEDE378\62285df8168b4aaa94cde91248074015\20.png


分享至 : QQ空间
收藏

0 个回复

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