标签:读取 group by UI binlog hosts sam opera into name
#再次不做过多介绍
#示例中department为部门表,employee为员工表。#
select 字段列 from 表1
inner|left|right join 表2 on
表1.字段 = 表2.字段
不适用任何匹配条件,生成笛卡尔积
第一个表的每一列对应后面表的所有列
找两张表共有的部分,相当于利用笛卡尔积结果中筛选除了正确的结果。
若一个表有而另一个表没有,则不会被匹配到。
mysql> select employee.id,employee.name,employee.age,employee.sex,department.name from employee inner join department on employee.dep_id=department.id;
或者
mysql> select employee.id,employee.name,employee.age,employee.sex,department.name from employee,department where employee.dep_id=department.id;
在内连接的基础上增加左边有而右边没有的数据
mysql> select employee.id,employee.name,department.name as depart_name from employee left join department on employee.dep_id=department.id;
在内连接的基础上增加右边有而左边没有的数据
mysql> select employee.id,employee.name,department.name as depart_name from employee right join department on employee.dep_id=department.id;
不管两个表是左边没有还是右边没有,都显示,没有的数据用null填充。
MySQL不支持全外链接。但是可以使用左右连接间接实现
select * from employee left join department on employee.dep_id = department.id
union
select * from employee right join department on employee.dep_id = department.id
;
#注意! union与union all的区别:union会去掉相同的纪录
以内连接的方式查询,在对应查询语句后加 where,order by等操作。
内连接,并过滤
select employee.name,department.name from employee inner join department
on employee.dep_id = department.id
where age > 25;
#以上面为基础,增加排序
select employee.id,employee.name,employee.age,department.name from employee,department
where employee.dep_id = department.id
and age > 25
order by age asc;
将一个查询语句嵌套在另一个查询语句中。
内层查询语句的结果,可以为外层查询语句提供条件。
子查询中可以包含:IN ,NOT IN ,ANY ,ALL ,EXISTS和NOT EXISTS等关键字。
还可以包含比较运算符:= , != , > , < 等。
# 查平均年龄25岁以上部门名
select id,name from department where id in (select dep_id from employee group by dep_id having avg(age) > 25);
#查技术部员工名
select name from employee where dep_id in (select id from department where name=‘技术‘);
#查看哪些部门不超过一人
select name from department where id in (select dep_id from employee group by dep_id having count(id) <=1);
比较运算符:=、!=、>、>=、<、<=、<>
# 输出大于所有人平均年龄的人的人名和年龄
select name,age from emp where age > (select avg(age)) from emp);
# 查询大于部门内平均年龄的员工名和年龄。
select name,age from emp t1
inner join (select dep_id,avg(age) avg_age from emp group by dep_id) t2
on t1.dep_id = t2.dep_id where t1.age > t2.avg_age;
过滤条件使用EXISTS时,内层查询语句不会反悔查询记录,只返回布尔值。true或false
返回true时外层语句才执行。
返回false时外层语句不执行。
#department表中存在dept_id=203,Ture
mysql> select * from employee
-> where exists
-> (select id from department where id=200);
+----+------------+--------+------+--------+
#department表中存在dept_id=205,False
mysql> select * from employee
-> where exists
-> (select id from department where id=204);
Empty set (0.00 sec)
mysql> SELECT DISTINCT CONCAT(‘User: ‘‘‘,user,‘‘‘@‘‘‘,host,‘‘‘;‘) AS query FROM mysql.user;
mysql> show grants for ‘cactiuser‘@‘%‘;
mysql> select * from mysql.user where user=‘cactiuser‘ \G
mysql> desc mysql.user;
GRANT USAGE ON . TO ‘jiudou‘@‘10.173.6.182‘ IDENTIFIED BY PASSWORD ‘9F4DCFD752EDB2DD095A080EEF9508444FF42894‘;
GRANT ALL PRIVILEGES ON stock_data
. TO ‘jiudou‘@‘10.173.6.182‘
连接(登陆)权限,建立一个用户,就会自动授予其usage权限(默认授予)。
mysql> grant usage on . to ‘p1′@’localhost’ identified by ‘123′;
该权限只能用于数据库登陆,不能执行任何操作;且usage权限不能被回收,也即REVOKE用户并不能删除用户。
必须有select的权限,才可以使用select table
mysql> grant select on pyt.* to ‘p1′@’localhost’;
mysql> select * from shop;
必须有create的权限,才可以使用create table
mysql> grant create on pyt.* to ‘p1′@’localhost’;
必须具有create routine的权限,才可以使用{create |alter|drop} {procedure|function}
mysql> grant create routine on pyt.* to ‘p1′@’localhost’;
当授予create routine时,自动授予EXECUTE, ALTER ROUTINE权限给它的创建者:
mysql> show grants for ‘p1′@’localhost’;
+—————————————————————————+
Grants for p1@localhost
+————————————————————————–+
| GRANT USAGE ON . TO ‘p1′@’localhost’ IDENTIFIED BY PASSWORD ‘*23AE809DDACAF96AF0FD78ED04B6A265E05AA257′ |
| GRANT SELECT, CREATE, CREATE ROUTINE ON pyt
.* TO ‘p1′@’localhost’|
| GRANT EXECUTE, ALTER ROUTINE ON PROCEDURE pyt
.pro_shop1
TO ‘p1′@’localhost’ |
+————————————————————————————-+
必须有create temporary tables的权限,才可以使用create temporary tables.
mysql> grant create temporary tables on pyt.* to ‘p1′@’localhost’;
[mysql@mydev ~]$ mysql -h localhost -u p1 -p pyt
mysql> create temporary table tt1(id int);
必须有create view的权限,才可以使用create view
mysql> grant create view on pyt.* to ‘p1′@’localhost’;
mysql> create view v_shop as select price from shop;
要使用CREATE USER,必须拥有mysql数据库的全局CREATE USER权限,或拥有INSERT权限。
mysql> grant create user on . to ‘p1′@’localhost’;
或:mysql> grant insert on . to p1@localhost;
必须有insert的权限,才可以使用insert into ….. values….
必须有alter的权限,才可以使用alter table
alter table shop modify dealer char(15);
必须具有alter routine的权限,才可以使用{alter |drop} {procedure|function}
mysql>grant alter routine on pyt.* to ‘p1′@’ localhost ‘;
mysql> drop procedure pro_shop;
Query OK, 0 rows affected (0.00 sec)
mysql> revoke alter routine on pyt.* from ‘p1′@’localhost’;
[mysql@mydev ~]$ mysql -h localhost -u p1 -p pyt
mysql> drop procedure pro_shop;
ERROR 1370 (42000): alter routine command denied to user ‘p1′@’localhost’ for routine ‘pyt.pro_shop’
必须有update的权限,才可以使用update table
mysql> update shop set price=3.5 where article=0001 and dealer=’A‘;
必须有delete的权限,才可以使用delete from ….where….(删除表中的记录)
必须有drop的权限,才可以使用drop database db_name; drop table tab_name;
drop view vi_name; drop index in_name;
通过show database只能看到你拥有的某些权限的数据库,除非你拥有全局SHOW DATABASES权限。
对于p1@localhost用户来说,没有对mysql数据库的权限,所以以此身份登陆查询时,无法看到mysql数据库:
mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema|
| pyt |
| test |
+——————–+
必须拥有show view权限,才能执行show create view。
mysql> grant show view on pyt.* to p1@localhost;
mysql> show create view v_shop;
必须拥有index权限,才能执行[create |drop] index
mysql> grant index on pyt.* to p1@localhost;
mysql> create index ix_shop on shop(article);
mysql> drop index ix_shop on shop;
执行存在的Functions,Procedures
mysql> call pro_shop1(0001,@a);
+———+
| article |
+———+
| 0001 |
| 0001 |
+———+
mysql> select @a;
+——+
| @a |
+——+
| 2 |
+——+
必须拥有lock tables权限,才可以使用lock tables
mysql> grant lock tables on pyt.* to p1@localhost;
mysql> lock tables a1 read;
mysql> unlock tables;
有了REFERENCES权限,用户就可以将其它表的一个字段作为某一个表的外键约束。
必须拥有reload权限,才可以执行flush [tables | logs | privileges]
mysql> grant reload on pyt.* to p1@localhost;
ERROR 1221 (HY000): Incorrect usage of DB GRANT and GLOBAL PRIVILEGES
mysql> grant reload on . to ‘p1′@’localhost’;
Query OK, 0 rows affected (0.00 sec)
mysql> flush tables;
拥有此权限可以查询master server、slave server状态。
mysql> show master status;
ERROR 1227 (42000): Access denied; you need the SUPER,REPLICATION CLIENT privilege for this operation
mysql> grant Replication client on . to p1@localhost;
或:mysql> grant super on . to p1@localhost;
mysql> show master status;
+——————+———-+————–+——————+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+——————+———-+————–+——————+
| mysql-bin.000006 | 2111 | | |
+——————+———-+————–+——————+
mysql> show slave status;
拥有此权限可以查看从服务器,从主服务器读取二进制日志。
mysql> show slave hosts;
ERROR 1227 (42000): Access denied; you need the REPLICATION SLAVE privilege for this operation
mysql> show binlog events;
ERROR 1227 (42000): Access denied; you need the REPLICATION SLAVE privilege for this operation
mysql> grant replication slave on . to p1@localhost;
mysql> show slave hosts;
Empty set (0.00 sec)
mysql>show binlog events;
+—————+——-+—————-+———–+————-+————–+
| Log_name | Pos | Event_type | Server_id| End_log_pos|Info | +—————+——-+————–+———–+————-+—————+
| mysql-bin.000005 | 4 | Format_desc | 1 | 98 | Server ver: 5.0.77-log, Binlog ver: 4 | |mysql-bin.000005|98|Query|1|197|use mysql
; create table a1(i int)engine=myisam|
……………………………………
关闭MySQL:
[mysql@mydev ~]$ mysqladmin shutdown
重新连接:
[mysql@mydev ~]$ mysql
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2)
[mysql@mydev ~]$ cd /u01/mysql/bin
[mysql@mydev bin]$ ./mysqld_safe &
[mysql@mydev bin]$ mysql
拥有grant option,就可以将自己拥有的权限授予其他用户(仅限于自己已经拥有的权限)
mysql> grant Grant option on pyt.* to p1@localhost;
mysql> grant select on pyt.* to p2@localhost;
拥有file权限才可以执行 select ..into outfile和load data infile…操作,但是不要把file, process, super权限授予管理员以外的账号,这样存在严重的安全隐患。
mysql> grant file on . to p1@localhost;
mysql> load data infile ‘/home/mysql/pet.txt’ into table pet;
这个权限允许用户终止任何查询;修改全局变量的SET语句;使用CHANGE MASTER,PURGE MASTER LOGS。
mysql> grant super on . to p1@localhost;
mysql> purge master logs before ‘mysql-bin.000006′;
通过这个权限,用户可以执行SHOW PROCESSLIST和KILL命令。默认情况下,每个用户都可以执行SHOW PROCESSLIST命令,但是只能查询本用户的进程。
mysql> show processlist;
+—-+——+———–+——+———+——+——-+——————+
| Id | User | Host | db | Command | Time | State | Info |
+—-+——+———–+——+———+——+——-+——————+
| 12 | p1 | localhost | pyt | Query | 0 | NULL | show processlist |
+—-+——+———–+——+———+——+——-+——————+
mysql> grant super on pyt.* to p1@localhost;
ERROR 1221 (HY000): Incorrect usage of DB GRANT and GLOBAL PRIVILEGES
mysql> grant super on . to p1@localhost;
Query OK, 0 rows affected (0.01 sec)
标签:读取 group by UI binlog hosts sam opera into name
原文地址:http://www.cnblogs.com/jinyudong/p/8011033.html