命令
删除数据
delete from tablename
truncate table tablename(删除效率特别高,适用于百万千万级别的数据)
导入数据
1、先下载数据并打开cmd进入保存文件的目录
2、输入命令
mysql -u 账户 -p <sql文件 之后输入账户的密码
例子:mysql -u root -p <employees.sql
查询命令
limit:最大查询
例子:select * from employees limit 5;
最大查询5行
条件过滤
并且(and)
select * from 数据库名称 where 查询的类目and 查询的条件
例子:select * from employees where first_name="georgi" and last_name="facello";
或者(or)
select * from 数据库名称 where 查询的类目 or 查询的条件
例子:select * from employees where first_name="georgi" or last_name="facello";
由于数据太多,所以后面加上limit 5;
包含(in)
select * from 数据库名称 where 查询的类目 or 查询的条件
例子:select * from employees where first_name in ("christ","lamba","asdfg");
由于数据太多所以添加只查看5行的命令limit 5;
范围检查(between and)
select * from 数据库名称 where 查询的类目between查询的田间 and 查询的条件
例子:select * from employees where hire_date between"1986-12-1"and"1986-12-31"
由于数据太多所以只看五行limit 5;
否定结果 (not in)
select * from 数据库名称 where 查询的类目 not in查询的条件
例子:select * from employees where first_name not in ("georgi") limit 5;
由于数据太多所以只看五行limit 5;
匹配任意字符(%)
select * from 数据库名称 where 查询的类目 like 查询的条件+%
例子:select * from employees where first_name like"georgi%" limit 5;
由于数据太多所以只看5行
中间为er,前后任意字符
select * from 数据库名称 where 查询的类目 like %er%
例子:select * from employees where first_name like"%or%" limit 5;
前面为er后面任意字符
select * from 数据库名称 where 查询的类目 like %erlimit5;
例子:select * from employees where first_name like"%er" limit 5;
前面为两个任意字符ka后面为任意字符
select * from 数据库名称 where 查询的类目 ___ka %erlimit5;
select * from employees where first_name like"__ka%" limit 5;
以什么开头(^)
select * from 数据库名称 where 查询的类目rlike '^mu'limit5;
例子:select * from employees where first_name rlike "^mu" limit 5;(以mu开头)
以什么结尾($)
select * from 数据库名称 where 查询的类目rlike '^mu'limit5;
例子:select * from employees where first_name rlike "mu$" limit 5;(以mu结束)
别名(as)
select 指定的字符串 as 要命令的新名字 from 那个数据库
例子: select count(1) as 总数 from employees;
定义count(1)为总数
对结果信息进行排序(desc正序/asc倒序)
select * rom salaris order by 要查询的字符串 desc(正序)/asc(倒序) limit 10;(数据多只查看10行)
正序例子: select * from salaries order by salary desc limit 5;
倒序例子: select * from salaries order by salary asc limit 5;
聚合排序
select 要查询的类别,count(1) as count from 从那个表里面group by 要查询的类别;
例子: select gender,count(1) as countt from employees group by gender;
将相同的聚合起来并进行排序
elect 要查询的类别,count(1) as count from salaries group by 要查询的类别 order by count 正序排列 limit 10;
例子:select salary,count(1) as count from salaries group by salary order by count desc limit 10;
总和(sum)
select sum(目标类目) from salaries;
例子:select sum(salary) from salaries;
最大(max)
select max(目标类目) from salaries;
例子:select max(salary) from salaries;
最小(min)
例子:select min(salary) from salaries;
平均(avg)
select avg(目标类目) from salaries;
例子:select avg(salary) from salaries;
按照年份对薪资从高到低进行排序
例子:select year(from_date) as dateyear,sum(salary) as sumsalary from salaries group by dateyear order by sumsalary desc limit 10;
以每一年的平均薪资进行从高到低的排序
例子:select year(from_date) as dateyear,avg(salary) as avg from salaries group by dateyear order by avg desc;
去重(distinct)
select 要查看的类别 from 表的名称;
例子:(查看字段)select title from titles limit 10;
↑
例子:(去重查看)select distinct title from titles limit 10;
过滤(having)
当我们在查询某项数据,需要过滤里面部分的内容时,使用的命令为:
select salary from salaries having salary>70000 limit 10;大于70000
select salary from salaries having salary<70000 limit 10;小于70000
select salary from salaries having salary=70000 limit 10;等于7000
select salary from salaries:从salaries中获取salary的数据;
having salary>/</=70000:取salary大于/小于/等于70000的数据;
如:找到平均薪资超过140000的员工,使用的命令为:
select emp_no,avg(salary) as avg from salaries group by emp_no having avg>140000 order by avg desc limit 10;
关联
把多个表通过一个唯一值进行关联起来的过程。
关联:表格的创建
如我们通过一个人的身份证信息(code)把一个人的个人信息(person)、工作经历(work)、教育经历(educate)三个表关联起来:
关联:数据的插入
插入一条数据,以身份证信息1001进行关联:
关联:数据的查询
内连接(inner join)
当我们要查询关联的表中的某些字段的内容时,我们需要通过内关联将有关联的表中的字段关联起来,使用的命令为:
select prtson.name,prtson.age,work.copany,educate.schoolname from prtson inner join work on prtson.code=work.code inner join educate on work.code=educate.code where prtson.code=1001;
prtson.name,prtson.age,work.copany,educate.schoolname:想要获得的内容的字段;
inner json:表示内关联,即所有表中共有的数据;
work on prtson.code=work.code:表示prtson和work两个表通过code关联;
on:表示通过什么进行关联;
where prtson.code=1001:表示提取code为1001的这条数据中的内容。