MySQL学习笔记(五):数据的操作、单表数据记录查询

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhuyunier/article/details/88354137

一、数据的操作

1、插入数据记录

(1)插入一条数据
【语法】

insert into table_name(field1,field2……fieldn) values(value1,value2……valuen);
insert into table_name values(value1,value2……valuen);

【实例】

#插入完整数据记录
mysql> insert into t_dept(deptno,dname,loc) values(1,'test4','zhongxi');
mysql> insert into t_dept values(33,'test5','zhongxi');
#插入数据记录一部分
mysql> insert into t_diary(tablename,diarytime) values('t_dept',now());

(2)插入多条数据
【语法】

insert into table_name(field1,field2……fieldn)
               values(value1,value2……valuen),
               values(value11,value12……value1n),
               values(value21,value22……value2n),
               …………
               values(valuem1,valuem2……valuemn);
               
insert into table_name
               values(value1,value2……valuen),
               values(value11,value12……value1n),
               values(value21,value22……value2n),
               …………
               values(valuem1,valuem2……valuemn);

【实例】

mysql> insert into t_dept
    ->   values(2,'test5','beiying'),
    ->         (4,'test6','zhongxi');
    
mysql> insert into t_diary(tablename,diarytime)
    ->   values('t_dept',now()),
    ->         ('t_dept',now());

(3)插入查询结果
【语法】

insert into table_name1(field11,field12……field1n)
 select (field21,field22……field2n)
   from table_name2 where ……

【实例】

mysql> insert into t_dept(dname,loc) select dname,loc from t_loader;

2、更新数据记录

(1)更新特定数据记录

【语法】

update table_name set field1=values1,field2=values2 where 条件语句;

【实例】

mysql> update t_dept set loc='beiying' where dname='dept4';

(2)更新所有数据记录
【语法】

update table_name set field1=values1,field2=values2 where 条件语句;
update table_name set field1=values1,field2=values2;

【实例】

mysql> update t_dept set loc='zhongxi' where deptno<4;
mysql> update t_dept set loc='shangxi';

3、删除数据记录

(1)删除特定数据记录

【语法】

delete from 表名 where 条件语句;

【实例】

mysql> delete from t_dept where dname='dept';

(2)删除所有数据记录

【语法】

delete from 表名 where 条件语句;

【实例】

mysql> delete from t_dept where deptno<4;

二、单表数据记录查询

1、简单数据记录查询
【语法】

select 字段名 from 表名;

(1)查询所有字段数据

mysql> select empno,ename,job,MSG,Hiredate,sal,comm,deptno from t_employee;
mysql> select * from t_employee;

(2)查询指定字段数据

mysql> select ename,comm from t_employee;

(3)避免重复数据查询-distinct

mysql> select distinct ename from t_employee;

(4)实现数学四则运算数据查询

mysql> select ename,sal*12 from t_employee;
#使用as给字段sal * 12重命名为yearsalary显示
mysql> select ename,sal * 12 as yearsalary from t_employee;

(5)设置显示格式数据查询

mysql> select concat(ename,'’s annual salary is :',sal * 12) yearsalary from t_employee;

2、条件数据记录查询
【语法】

select 字段名 from 表名 where 条件语句; 

(1)带关系运算符和逻辑运算符的条件数据查询

  • 单条件数据查询
mysql> select ename from t_employee where job='job2';
  • 多条件数据查询
mysql> select ename from t_employee where job='job1'&&sal>1;

(2)带between and关键字的范围查询
【语法】

select 字段名 from 表名 where between value1 and value2; 
  • 符合范围的数据记录查询
mysql> select ename from t_employee where sal between 2 and 3;
  • 不符合范围的数据记录查询
mysql> select ename from t_employee where sal not between 2 and 3;

(3)带is null 关键字的空值查询
【语法】

select 字段名 from 表名 where 字段名 is null; 
  • 空值数据记录查询
mysql> select ename from t_employee where comm is null;
  • 不是空值数据记录查询
mysql> select ename from t_employee where not comm is null;

(4)带in关键字的集合查询
【语法】

select 字段名 from 表名 where field in (value1,value2……,valuen); 
  • 在集合中数据记录查询
mysql> select ename from t_employee where sal in (2,7,8);
mysql> select ename from t_employee where sal=2 or sal=7 or sal=8;
  • 不在集合中数据记录查询
mysql> select ename from t_employee where not sal in (2,7,8);
mysql> select ename from t_employee where sal not in (2,7,8);
  • 关于集合查询注意点
#使用关键字in时,查询集合中存在null,不影响查询
mysql> select ename from t_employee where sal in (2,7,8,null);
+--------+
| ename  |
+--------+
| ename2 |
| ename1 |
+--------+
2 rows in set (0.00 sec)

#使用关键字not in时,查询集合中存在null,不会有任何查询结果
mysql> select ename from t_employee where sal not in (2,7,8,null);
Empty set (0.00 sec)

(5)带like关键字的模糊查询
【语法】

select 字段名 from 表名 where field like value;
  • 带有”%“通配符的查询(该通配符能够匹配任意长度的字符串)
mysql> select ename from t_employee where ename like 'e%';
mysql> select ename from t_employee where ename not like 'e%';
  • 带有”_“通配符的查询(该通配符能匹配单个字符)
mysql> select ename from t_employee where ename like '_n%';
mysql> select ename from t_employee where ename not like '_n%';

(5)排序数据记录查询
【语法】

select 字段名 from 表名 where 条件语句 order by 字段 [asc|desc];
  • 按照单子段排序
#升序排序
mysql> select * from t_employee order by sal;
mysql> select * from t_employee order by sal asc;
#降序排序
mysql> select * from t_employee order by sal desc;
  • 按照多字段排序
mysql> select * from t_employee order by sal,MSG desc;
mysql> select * from t_employee order by sal asc ,MSG desc;

3、限制数据记录查询数量
【语法】

select 字段名 from 表名 where 条件语句 limit offset_start,row_count;

(1)不指定初始位置

mysql> select * from t_employee where comm is null limit 2; 

(2)指定初始位置

mysql> select * from t_employee where comm is null order by MSG limit 0,5;
mysql> select * from t_employee where comm is null order by MSG limit 5,10;

4、统计函数和分组数据记录查询
【语法】

select 统计函数(字段) from 表名 where 条件语句;

【统计函数】

  • count()函数:统计表中记录的条数
  • avg()函数:计算字段值的平均值
  • sum()函数:计算字段值的总和
  • max()函数:查询字段值的最大值
  • min()函数:查询字段值的最小值
  • 如果所操作的表里没有任何数据,则count()函数返回数据0,其他函数则返回null

(1)mysql支持的统计函数

  • 统计数据记录条数
mysql> select count(*) number from t_employee;

mysql> select count(comm) number from t_employee;

mysql> select count(comm) number from t_employee where not comm=0;
  • 统计计算平均值
mysql> select avg(comm) average from t_employee where not comm=0;
  • 统计计算求和
mysql> select sum(comm) sumvalue from t_employee where not comm=0;
  • 统计计算最大值和最小值
mysql> select max(sal) maxval,min(sal) minval from t_employee;

(2)分组数据查询

  • 简单分组查询(分组中显示的数据记录为随机的)
    【语法】
select 统计函数 from 表名 where 条件语句 group by 字段名;

【实例】

mysql> select * from t_employee group by deptno;
  • 实现统计功能分组查询

【语法】

select group_concat(字段名) from 表名 where 条件语句 group by 字段名;

【实例】

mysql> select deptno,group_concat(ename) enames,count(ename) number 
    ->   from t_employee group by deptno;
  • 实现多个字段分组查询

【语法】

select group_concat(field),function(field) from 表名 where 条件语句 group by field1……fieldn;

【实例】

mysql> select deptno,MSG,group_concat(ename) enames,count(ename) number
    ->  from t_employee group by deptno,MSG;
  • 实现having子句限定分组查询

【语法】

select function(field) from 表名 where 条件语句 group by field1……fieldn having 条件语句;

【实例】

mysql> select deptno,avg(sal) average,group_concat(ename) enames,count(ename) number
    ->  from t_employee
    ->   group by deptno
    ->     having avg(sal) > 3;

猜你喜欢

转载自blog.csdn.net/zhuyunier/article/details/88354137