找回密码
 立即注册

推荐阅读

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

软件测试__mysql基本语句查询

[复制链接]
单表语句:(1).查询一个表中所有的数据
格式: select * from 表名;
select * from cs2;

(2)查询表中具体的字段
格式:select   字段名1,字段名2,字段名3  FROM   表名 ;   
select name , age ,class from cs2;


(3)查询表中具体的字段取别名(别名是为了在查看数据是方便)
格式:select   字段1 as "姓名",字段2 "年龄" FROM   表,名;
select name as  "姓名",age as  "年龄" from cs2;

select name "姓名",age "年龄" from cs2;
注意:as可以省略

(4)查询指定内容 ,用where+条件
格式:select  *   from   表名   where  字段名=字段 ;

select * from cs2 where age=23;

where条件使用注意:
比较运算符
(1)where  +条件(=(等于), !=(不等于) ,<>(不等于) ,<(小于),>(大于),>=(大于等于),<=(小于等于))
select  * from   cs2 where  age=23 ;
select  * from   cs2 where  age=23
select  * from   cs2 where  age<>23 ;
select  * from   cs2 where  age>23 ;
select  * from   cs2 where  age<23 ;
select  * from   cs2 where  age<=23 ;
select  * from   cs2 where  age>=23 ;


(2)and (与)
and  同时满足所有条件,比如:条件1和条件2都要满足
select * from cs2 where age<23 and age>15;
(3)or(或)
or 当有多个条件时满足其中任意1个条件都显示,
条件1和条件同时存在,只要满足条件1就显示,只要满足条件2就显示。
select * from cs2 where age>20 or age <15;
(4)between..... and....  在什么范围之间
格式:select  *   from   表名  where  表字段 BETWEEN 值得范围    and    值的范围
select * from cs2 where age between 15 and 20;
(5)in  在一组数据中匹配数据
格式:select  * from   表名    where    字段名 in (数组值1,数组值2,数组值3);
select * from cs2 where age in(23,24,15);

(6)is  null 为空  或is  not  null  非空
6.1  is  not  null  非空
格式:select  * from   表名    where  字段名  is not null ;
select * from cs2 where age is not null;


6.2  is    null  为空
格式:
select  * from   表名    where  字段名  is     null ;
select * from cs2 where age is null;


多行注释:ctrl+/
取消多行注释:ctrl+shift+/
单行注释:直接#号
排序:  
对表中的数据进行拍讯:order  by        asc 升序(可以省略)    desc   降序
降序:
格式:
select  *  from   表名   order  by  字段名    desc ;
select * from cs2 order by age desc;
升序:
格式:select  * from   表名   order  by  字段名   asc ;
select * from cs2 order by age asc;
select * from cs2 order by age ;asc省略默认升序



二次排序:
格式:select  * from   表名  order  by   字段1 desc ,字段2   asc ;
select * from cs2 order by age desc ,math asc;  先年龄降序,再 数学升序



模糊匹配查询  like
%:表会匹配0个字符或多个字符
_:表示一个字符
select * from cs2 where age like "1%"  #匹配1开头的部门编号数据
select * from cs2 where age like "%1%"#匹配的部门包含1编号数据
select * from cs2 where age like "%1"#匹配结尾包含1编号数据
select * from cs2 where age like "_1"#匹配固定的字符1编号数据,及一个尾数是1的两位数_1.
限制查询(limit)  后面接连个值  ,第一个值  表示下标(索引),  第二值是步长
备注:一个表中索引是从0开始
格式:select  *   from   表名   limit  索引,行数 ;
select * from cs2 limit 5 ;#默认索引为0
select * from cs2 limit 4,3;#表示索引是4 表示从第五行开始(包括第五行)往下数3行,及第5、6、7行
分组查询 group  by
group  by    一般和having   组合
select class,max(age) from cs2 group by class; #前提条件是班级,找到年龄的最大值
注意group by 一般不会单独使用,通常都是和函数having+条件使用。
group by 后查询出来的结果,在需要的条件下可以接having
sql语句函数:
max 最大值
select class,max(age) from cs2
min 最小值
select class,min(age) from cs2
avg 平均值
select class,avg(age) from cs2;
count 统计总数
select count(age) from cs2;
sum 求和
SELECT sum(age) from  cs2
distinct  去重
案例:SELECT  distinct(age) from cs2l
==========================
改:
update .... set....
格式:update 表明 set 修改的字段名=字段新值 where 条件
update cs2 set math=80 where age=20; 将年龄是20岁的math成绩改为80
==================================
删:
delete 删
truncate 快速删除表内数据
drop 删除
删除数据速度:drop> truncate>delete

注意:
1.drop是删除表和数据
2.truncate 删除无法恢复
3.delete删除是可以恢复
delete:
1)删除表中所有数据
     格式:delete from 表名
2)删除表中指定条件的数据
     格式: delete from 表名 where 条件
    eg:delete from cs2 where name='小明'
3)快速删除表数据
   格式:truncate 表名
====================================
表取别名:
案例:select a.name,a.age from cs2 as a; 表示表名cs2 临时用a表示
==========================
备份表结构:
格式:create table 新表名 like 备份原表名
create table cs3 like cs2;  #备份cs2的表格结构
备份部分结构和数据:
格式:insert into 表名(字段名1,字段名2) select (字段名1,字段名2) from 要备份的表名
eg:insert into cs3(id,age)  select  id ,age from cs2; 注意如果是往空表里面备份数据主要空表有没有主键,右的话要导入主键,主键不能为空。
备份表结构和数据:
格式: create table 表名 as (select * from 备份原表)
eg:create table cs4 as (select * from cs2); #创建cs4并将cs2中的结构和数据同事导入cs4中。
=====================
linux备份数据库
备份用:'>'
格式:mysqldump -u root -p 原数据库> 数据库脚本.后缀名
eg:mysqldump -u root -p mysql>/home/mysqlbf.sql

分享至 : QQ空间
收藏

0 个回复

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