四、表的增删改查
1.插入并同步更新:
insert into student(id,sn,name)values(100,1000,‘唐大师’)on duplicate key updata sn=1000,name=‘唐大师’;
2.替换:replace into student(sn,name) values (2001,'曹阿瞒');
3.指定列查询:select 字段名 from 表名;
4.多字段查询:select id,name,Chinese+math+English from from 表名;
5.为查询结果指定别名: select id,name,Chinese+english+math 总分 from 表名;
6.结果去重:select distinct 字段名 from 表名;
7.where条件查询
查询小于60:select name ,yingyu from 表名 where yingyu<60;
查询两者之间:select name,yingyu from 表名 where yingyu>=80 and yingyu<=90;
查询或:select name,yingyu from 表名 where yingyu=58 or yingyu=59 or yingyu=60;
用in查询:select name,yingyu from 表名 where yingyu in (58,59,60);
查询任意多个字符:select name from 表名 where name like '孙%';
查询严格的任意一个字符: select name from 表名 where name like '孙_';
8.结果排序:
升序:select name,qq from students order by qq asc; asc和系统默认排序都为升序
降序:select name,qq from students order by qq desc;
9.筛选分页结果:
select id ,name,shuxue,yingyu,yuwen [order by id] limit n offset s; 表示1从s开始筛选n条结果
10.更新(对查询到结果进行更新):
select name, shuxue from 表名 where name ='孙悟空';
updata 表名 set shuxue =80 where name='孙悟空';
11.删除数据
删除指定的,例如:delete from 表名 where name= '孙悟空';
删除整个表的内容: delete from 表名
12.截断表: truncate 表名
13.聚合函数:
统计:select count(x) from 表名;x表示要查的对象
求和:select sum(x) from 表名;x表示要查的对象
求平均:select avg(x) from 表名;x表示要查的对象
求最小的:select min(x) from 表名;x表示要查的对象
14.创建一个表和另一个表的结构一样:
create table 表名 like 另一表名,
insert into 表名 select distinct * from 另一表名;
15.日期函数:
获得年月日:select current_date();
获得时分秒:select current_time();
获得时间戳:select current_timestamp();
时间相加:select date_add(,); 时间相减:select date_sub(,); 计算日期之差:select datediff(,);
16.字符串函数:
获取表的某一列字符集:select charset(字段名) from 表名
select concat(name,‘的语文是’,Chinese,‘分’) as ‘分数’ from 表名;
求学生表中学生姓名占用的字节:select length(name),name from student;
将‘s'替换为’上海‘; select replace(ename,’s',‘上海’),ename from 表名;
以首字母小写的方式显示员工信息:select concat(case(substring(ename,1,1)),substring(ename ,2)) from Emp;
17.数学函数:
绝对值:select abs(); 向上取整:select ceiling(); 向下取整:select floor(); 保留两位小数select format(12.3456,2);
产生随机数: select rand();
查询当前用户:select user(); 显示当前使用数据库:select database(); 对用户加密的函数:select password(‘root’);