数据操作-对数据的增删改查-单表查询和多表查询

补漏:mysql> select id from course where cname = '物理'; 这个得出来的结果显示是一个表,其实是mysql为了显示好看设计出来的,
结果就是一个数据类型,比如id就是一个整型。


1.上课笔记

today my level is one

difficult knowledge

1.only_full_group_by
sql语句不区分大小写
对数据操作
1.增删改
方法:查询记录当作结果传入新表
权限操作
select * from table\G 逐行显示
Host:% 所有的ip都行,代表账号可以在任意的主机上都能链接
2.单表查询(*******)
完整语法(语法级别关键字顺序)
select distinct 。。。 from tables
where整体筛选
group by查询出来的结果按照规定分类
having分完组过滤(分类后筛选) ==》运行distinct
order by 排序的字段
limit 限制显示的条数
去重是针对一个字段
表头 as改表头名字
concat('年龄:',name,'姓名:',name)***
concat_ws(':',name,age) name和age之间用:拼接起来

between 10 and 15 包含10和15
where没有明确条件
分组之后才能用聚合函数
select max(age) from emp;可以使用max(),因为没有where,默认为True,自动成一组
group by

 


 1、跟权限有关的几张表
user->db->table_priv->columns_priv

 2、只创建账号
create user tom@"客户端的ip" identified by "123";
create user tom@"192.168.15.%" identified by "123";
create user tom@"%" identified by "123";

客户端:192.168.15.13 服务端:192.168.15.90

create user tom@"192.168.15.13 " identified by "123";
mysql -utom -p"123" -h 192.168.15.90 -P 3306

 3、创建账号并且授权(只有root账号才能为其他账号授权grant)
 *.* ===> mysql.user
grant all on *.* to "tom"@"192.168.15.90" identified by "123";#all代表除了grant以外的所有权限

 db1.* ===> mysql.db
grant all on db1.* to "jack"@"192.168.15.90" identified by "123";#all代表除了grant以外的所有权限

 db1.t1 ===> mysql.tables_priv
grant all on db1.t1 to "rose"@"192.168.15.90" identified by "123";#all代表除了grant以外的所有权限

 db1.t1(id) ===> mysql.columns_priv
grant select(id),update(name) on db1.t1 to "lili"@"192.168.15.90" identified by "123";


 修改完权限一定要
flush privileges;


drop user "tom"@"192.168.15.90";
drop user "jack"@"192.168.15.90";
drop user "rose"@"192.168.15.90";
drop user "lili"@"192.168.15.90";
flush privileges;

 

1、完整语法(语法级别关键字的排列顺序如下)
select distinct 字段1,字段2,字段3,... from 库名.表名
where 约束条件
group by 分组依据
having 过滤条件
order by 排序的字段
limit 限制显示的条数
;
 必须要有的关键字如下:
select * from t1;


 关键字执行的优先级
from
where
group by
having
distinct
order by
limit


def from():
pass

def where():
pass

def group():
pass

def having():
pass

def distinct():
pass

def order():
pass

def limit():
pass

def select():
f=from()
res1=where(f)
res2=group(res1)
res3=having(res2)
res4=distinct(res3)
res5=order(res4)
limit(res5)

 


1、简单查询
select * from t1;
select id,name,sex from t1;
select distinct post from emp;
select name,salary*12 as annual_salary from emp;

select concat('名字: ',name) as new_name,concat("年龄: ",age) as new_age from emp;
select concat(name,":",age) from emp;

select concat(name,":",age,":",sex) from emp;
select concat_ws(":",name,age,sex) as info from emp;


SELECT
(
CASE
WHEN NAME = 'egon' THEN
NAME
WHEN NAME = 'alex' THEN
CONCAT(name,'_BIGSB')
ELSE
concat(NAME, 'SB')
END
) as new_name
FROM
emp;

2、where
select * from emp where id >= 10 and id <=15; # 等同于select * from emp where id between 10 and 15;
select * from emp where id = 6 or id = 9 or id = 12; # 等同于select * from emp where id in (6,9,12);

_代表任意单个字符
%代表任意无穷个字符
select * from emp where name like "__";
select * from emp where name like "jin%";
select * from emp where id not in (6,9,12);
select * from emp where id not between 10 and 15;


3、group by分组
 什么分组:按照所有记录相同的部分进行归类,一定区分度低的字段
 为何要分组:当我们要以组为单位进行统计时就必须分组,分组的目的是为了以组为单位进行统计的,再去考虑单条记录毫无意义


 set global sql_mode="strict_trans_tables,only_full_group_by";
 注意:分组之后,只能查到分组的字段以及组内多条记录聚合的成果

select * from emp group by post;

 聚合函数
max
min
avg
sum
count


select post,count(id) from emp group by post;
select post,max(salary) from emp group by post;
select post,avg(salary) from emp group by post;
select sex,count(sex) from emp group by sex;

 统计出每个部门年龄30以上的员工的平均薪资
select post,avg(salary) from emp where age >= 30 group by post;

 注意:分组是在where之后发生的
mysql> select * from emp where max(salary) > 3000;
ERROR 1111 (HY000): Invalid use of group function

 group_concat
select post,group_concat(name,':',age) from emp group by post;

 4、having 过滤条件

 where是在分组之前的过滤,即在分组之前做了一次整体性的筛选
having是在分组之后的过滤,即在分组之后专门针对聚合的结果进行进一步的筛选

select post,avg(salary) from emp group by post having avg(salary) > 10000;
select post,avg(salary) from emp group by post ;

5、order by排序

select * from emp order by age asc; # 默认asc升序-》从小到大
select * from emp order by age desc;# desc降序-》从大到小

select * from emp order by age asc,salary desc; # 先按照age升序排列,如果age相同则按照salary降序排

select post,avg(salary) from emp group by post order by avg(salary);

6、limit 限制显示的条件
select * from emp limit 3;

薪资最高那个人的详细信息
select * from emp order by salary desc limit 1;

分页显示
select * from emp limit 0,5; # 从0开始往后取5条
select * from emp limit 5,5; #从5开始往后取5条

正则表达式
select * from emp where name regexp "^jin.*(g|n)$";

多表查询
1、笛卡儿积
select * from emp,dep;

select * from emp,dep where emp.dep_id = dep.id;

select * from emp,dep where emp.dep_id = dep.id and dep.name = "技术";

2、内连接:只取两张表有对应关系的记录
select * from emp inner join dep on emp.dep_id = dep.id;
select * from emp inner join dep on emp.dep_id = dep.id
where dep.name = "技术";


3、左连接: 在内连接的基础上保留左表没有对应关系的记录
select * from emp left join dep on emp.dep_id = dep.id;

4、右连接: 在内连接的基础上保留右表没有对应关系的记录
select * from emp right join dep on emp.dep_id = dep.id;

5、全连接:在内连接的基础上保留左、右面表没有对应关系的的记录
select * from emp left join dep on emp.dep_id = dep.id
union
select * from emp right join dep on emp.dep_id = dep.id;

子查询
子查询:就是将一个查询语句的结果用括号括起来当作另外一个查询语句的条件去用


select * from emp where dep_id in (select id from dep where name = "技术" or name = "人力资源");


每个部门最新入职的员工
select t1.id,t1.name,t1.hire_date,t1.post,t2.* from emp as t1
inner join
(select post,max(hire_date) as max_date from emp group by post) as t2
on t1.post = t2.post
where t1.hire_date = t2.max_date
;

select * from emp inner join dep on emp.dep_id = dep.id;

 

posted @ 2018-07-23 20:23  Roc_Atlantis  阅读(308)  评论(0编辑  收藏  举报