SET NAMES UTF8/GBK;
SOURCE 盘符://sql文件.sql
在cmd窗口中 不要登录进数据库
mysqldump -u用户名 -p备份库名 > 存放地址后回车,输入数据库密码
mysqldump -u用户名 -p - - databases 库名 库名 > 存放地址
mysqldump -u用户名 -p - - all-databases > 存放地址
恢复时要先创建库,因为备份的时候不会备份库名
mysql -u用户名 -p 库名 < 存放地址
先进去库中,source 存放地址
函数 | 说明 |
---|---|
celi(条件) | 向上整取 |
floor(条件) | 向下整取 |
round(条件) | 向下整取 |
rand(条件) | 随机数 |
select 列名,数值函数(条件) from 表名;
-- emp表中所有的员工薪资上涨15.47%并向上整取
select name,sal,celi(sql*1.1547) from emp;
函数 | 说明 |
---|---|
curdate() | 获取当前日期,年月日 |
curtime() | 获取当前时间,时分秒 |
sysdate() | 获取当前日期+时间,年月日时分秒 |
now() | 获取当前日期+时间,年月日时分秒 |
year(date) | 返回date中的年 |
month(date) | 返回date中的月 |
day(date) | 返回date中的日 |
hour(date) | 返回date中的时 |
minute(date) | 返回date中的分 |
second(date) | 返回date中的秒 |
date_add() | 增加日期 |
date_sub() | 减少日期 |
-- 查询emp表中在1993-1995年期间出生的员工,显示名字和出生日期
select name,birthday from emp where birthday between '1993-1-1' and '1995-12-31';
select name,birthday from emp where year(birthday) between 1993 and 1995;
函数 | 说明 |
---|---|
concat(s1,s2…) | 将s1s2多个字符串合并成一个字符串 |
concat_ws(x,s1,s2…) | 同concat函数,此函数每个字符串之间要加上x,x时分隔符 |
-- 查询emp表中员工的姓名和薪资,格式xxx(元)
select name,concat(sal,'(元)') from emp;
show databases;
use test;
show tables;
frop database if existsmydb1;
create database if not exists mydb1 charaset utf-8;
show create database mydb1;
drop table if exists stu;
create table stu (id int ,name varchar(50),gender varchar(10),birthday date,score double) charset utf8;
show create table stu;
desc stu;
select database();
insert into stu (id,name,gender,birthday,score) values(1,'tom','男','1999-01-01',85);
insert into stu values(2,'jack','女','2000-01-01',85);
insert into stu values(3,'zhangsan','女','2001-01-01',85),(4,'lisi','女','2000-01-01',85),(5,'wangwu','女','2000-01-01',85)...;
update stu set score = score+10;
delete from stu;
select name,sal,bonus from emp;
select distinct dept ,job from emp;
select name , sal from emp where sal >3000;
select name 姓名, sal+if null(bonus,0) 总薪资 from emp where (sal+ if null(bonus,0))>3500;
select name , sal from emp where sal between 3000 and 4500;
select name , sal from emp where sal in (1400,1600,1800);
select name , sal from emp where sal not in (1400,1600,1800);
select name ,sal from emp where sal > 4000 or sal<2000;
select name ,sal ,bonus from emp where sal > 3000 and if null(bonus,0)<600;
select name from emp where dept is null;
select name from emp where name like '刘%';
select name from emp where name like '%涛%';
select name from emp where name like '刘_';
select count(*) from emp where sal >3000;
select max(sal),min(sal) from emp;
select sum(sal),avg(sal) from emp;
select dept, count(*) from emp group by dept;
select job , count(*) from emp group by job;
select dept,max(sql) from emp group by dept;
-- 升序排列时 asc可省略
select name ,sal from emp order by sal asc;
select name ,bonus from emp order by bonus desc;
select * from emp limit 3,3;
select * from emp limit 0,3;
select name,birthday from emp where birthday between '1993-1-1' and '1995-12-31';
select name,birthday from emp where year(birthdat) between 1993 and 1995;
select * from emp where month(now()) = month(birthday);
select * from emp where (month(now())+1%12=month(birthday)%12;
select name ,concat(sal,'(元)) from emp;
select * from emp , dept where emp.dept_id = dept.id;
select * from dept inner join emp on emp.dept_id = dept.id;
select * from dept left join emp on emp.dept_id = dept.id;
select * from dept right join emp on emp.dept_id = dept.id;
select d.name,e.name from dept d,emp e where d.id = e.dept_id and d.name='培优部';
select e1.name,e2.id,e2.name from emp e1,emp e2 where e1.topid = e2.id;
select job,min(sal) from emp group by job having min(sal)>1500;
select dept_id,count(*),avg(sal) from emp group by dept_id;
select e1.id,e1.name,d.name from emp e1,emp e2,dept d where e1.topid = e2.id and e1.dept_id = d.id and e1.hdate<e2.hdate;
select d.id,d.name,d.loc,count(*) from emp e,dept d where e.dept_id = d.id group by e.dept_id;
select dept_id,name,sal from emp;
select dept_id,max(sal) from emp gropy by dept_id;
select t1.dept_id,t1.maxsal,emp.name from emp ,(select dept_id,max(sal) maxsal from emp group by dept_id) t1 where emp.dept_id = t1.dept_id and emp.sal = t1.maxsal;
select name,sal from emp where sal >(select sal from emp where name = '王海涛');
select name, job from emp where job = (select job from emp where name ='刘佩霞');