找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手

Linux的用语 :查询 ,排序, 备份表,备份库

[复制链接]
查询==selest


select * from b11;==》查询全表数据,*代表所有
select name from b11;==》查询单个字段值
select name,phone from b11;==》查询多个字段值
select * from b11 where name="zhf";==》查询name为zhf的所有数据
select * from b11 where name!="zhf";==》查询name不等于zhf的所有数据
select * from b11 where name <> "zhf";==》查询name不等于zhf的所有数据
select * from b11 where name=:"xiaoliu" and class=1003; ==》查询name为xiaoliu并且class为1003的所有数据
select * from b11 where name="zhf" or name="hai";==》查询name为zhf或者为hai的所有数据
select * from b11 where id>2 and id<5;==》查询id大于2和小于5的数据
select * from b11 where id>=2 and id<=5;==》查询id大于等于2和小于等于5的数据
select * from b11 where id between 2 and 5;==》查询id大于等于2和小于等于5的数据
select * from b11 where phone in(13588888888,13522222222);==》查询phone为13588888888或13522222222的所有数据
select * from b11 where phone not in(13588888888,12345);==》查询phone不在13588888888或12345里面的数据
select * from b11 where class is null;==》查询class为空的数据
select * from b11 where class is not null;==》查询class不为空的数据
0不等于null null前面只能is null,is not null;

select * from b11 where name like "%zh%";==》查询name字段值有包含zh的所有数据
select * from b11 where name like "z%";==》查询name字段值以z开头的所有数据
select * from b11 where name like "%1";==》查询name字段以1结尾的所有数据
select * from b11 name limit 1,4;==》查询2到5行数据
select * from b11 id limit 0,5;==》查询前5行数据
limit m, n(m为下标,n为查询行数)


排序
从小到大- 升序 asc
select * from b11 order by phone asc;
从大到小 -降序 desc
select * from b11 order by phone desc;
分组
select * from b11 group by calss;==》通过calss字段进行分组
select class,count(*)from b11 group by calss;==》通过class字段分组然后求出每组对应人数
select class,count(*) from test group by class having class is not null;==》通过class字段进行分组,然后加条件class 不为null

注:
1.group by 分组之后,只能使用having进行条件筛选
2.使用group by 分组后,仅有分组字段和函数放置到from前面
常用的聚合函数


count()---统计
sum()---求和
avg()---求平均值
max()---最大值
min()---最小值
distinct()---去重

select count(*)from b11;==》统计b11表中数据条数
select sum(id)from b11 where name="zhf1";==》求name为zhf1的数据id之和
select avg(id)from b11 where name="zhf1";==》求name为zhf1的数据id的平均值
select max(phone) from b11;==》求phone字段最大值
select min(phone) from b11;==》求phone字段最小值
select distinct(name)from b11;==》对name字段去掉重复数据

备份表
create table b12 like b11;==》创建b12表,表结构跟b11表一致
insert into b12 select * from b11;==》往b12表里面插入b11表中的所有数据
insert into b13(name,class)select name,class from b11;
注意点 1.插入的表必须存在
2.插入的表是新表,没有数据

备份库 -(linux界面操作)
mysqldump -uroot -p123456 dcs9>/dcs9/dcs9.sql==》把dcs9这个库备份放到dcs9目录下的dcs9.sql文件中
mysql -uroot -p123456 dcs9_bak注意:dcs9_bak 要先存在,不然会找不到库


分享至 : QQ空间
收藏

0 个回复

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