mysql数据操做

了解:Mysql 帐号相关mysql

建立帐号:正则表达式

权限:user(全部库的权限)-->db(某个库的权限)-->table_priv(某张表的权限)sql

                  -->columns_oriv(某个字段的权限)ide

建立帐号:create user ‘tom’(帐号)@’客户端ip(%表明全部ip均可)’ identified by ‘123’(密码)函数

登陆:mysql -utom -p’123’ -h 服务端ip -P3306post

建立帐号并受权:只有root帐号能为其它帐号受权regexp

grant all(不包括grant权限) on *.* to ‘tom’@’%’ identified by ‘123’排序

其中:ip

*.*表明user权限资源

db.*表明db权限

db.t1表明table_priv权限

grant select(id) on db.t1 表明columns_oriv权限

# 修改完权限必定要

flush privileges;

 

单表查询

完整语法

select distinct 字段1,字段2,字段3,... from 库名.表名

                    where 约束条件

                    group by 分组依据

                    having 过滤条件

                    order by 排序的字段

                    limit 限制显示的条数

关键字执行优先级:

from    where    group by    having    distinct    order by    limit

 

字符拼接:

select concat('名字: ',name) as new_name,concat("年龄: ",age) as new_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;

where关键字

         _表明任意单个字符

         %表明任意无穷个字符

         select * from emp where name like "__";

         select * from emp where name like "jin%";

         select * from emp where id not in (6,9,12);

 

group by分组

         什么是分组:按照全部记录相同的部分进行归类,必定区分度低的字段

         为什么要分组:当咱们要以组为单位进行统计时就必须分组,分组的目的是为了以组为单位进行统计的,再去考虑单条记录毫无心义

 

         set global sql_mode="strict_trans_tables,only_full_group_by"; 设置严格模式

         注意:分组以后,只能查到分组的字段以及组内多条记录聚合的成果

         select * from emp group by post;

 

聚合函数

         max

         min

         avg

         sum

         count

 

having 过滤条件

         where是在分组以前的过滤,即在分组以前作了一次总体性的筛选

         having是在分组以后的过滤,即在分组以后专门针对聚合的结果进行进一步的筛选

 

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降序排

 

limit 限制显示的条件

         select * from emp limit 3;

         分页显示

         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)$";

 

多表查询

 

一、笛卡儿积

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 = "技术";

 

二、内链接:只取两张表有对应关系的记录

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 = "技术";

 

 

三、左链接: 在内链接的基础上保留左表没有对应关系的记录

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

 

四、右链接: 在内链接的基础上保留右表没有对应关系的记录

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

 

五、全链接:在内链接的基础上保留左、右面表没有对应关系的的记录

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 = "人力资源");

相关文章
相关标签/搜索