1、参考MySQL安装(8.0版本)
2、MySQL(5.7版本)
1.解压MySQL
2.修改my.ini文件
3.mysqld install
4.mysqld --initialize --console
5.net start mysql
6.mysql -P 3306 -u root -p
7.SET PASSWORD = PASSWORD(“123456”);
8.Navicat链接测试
如果MySQL已经安装过,那么执行mysqld remove
3、mysql默认数据库里面的四张表
1、user表(用户层权限)
2、db表(数据库层权限)
3、tables_priv表(表层权限)
4、columns_priv表(字段层权限)
命令:create user 用户名;
create user cxy;
select host,user,authentication_string from user;
命令:drop user 用户名;
drop user cxy;
命令:ALTER USER 用户 IDENTIFIED WITH mysql_native_password BY ‘密码’;
ALTER USER ‘ls’@‘%’ IDENTIFIED WITH mysql_native_password BY ‘123456’;
命令:flush privileges;
语法:grant privileges(用户的操作权限) on databasename(数据库名).tablename(表名) to username@‘host’;
6.1 给cxy用户 赋予 数据库db_cxy中的表tb_user查询权限
grant SELECT on db_cxy.tb_user to cxy@‘%’;
6.2 给 cxy用户 赋予 数据库db_cxy中的表tb_user 修改权限
grant UPDATE on db_cxy.tb_user to cxy@‘%’;
6.3 给 cxy用户 赋予 数据库db_cxy中所有表 查询权限
grant SELECT on db_cxy.* to cxy@‘%’;
6.4 给 cxy用户 赋予 数据库db_cxy中所有表 所有权限
grant all on db_cxy.* to cxy@‘%’;
语法:revoke privileges on databasename.tablename from username@‘host’;
7.1 啥也不能回收,不会对GRANT ALL PRIVILEGES ONdb_cxy
.* TOcxy
@%
有任何影响
revoke DELETE on db_cxy.tb_user from cxy@‘%’;
7.2 可以回收GRANT SELECT, UPDATE ONdb_cxy
.tb_user
TOcxy
@%
这条权限语句
revoke all on db_cxy.tb_user from cxy@‘%’;
7.3 可以回收GRANT ALL PRIVILEGES ONdb_cxy
.* TOcxy
@%
这条赋权语句带来的权限
revoke all on db_cxy.* from cxy@‘%’;
注:revoke只能回收grants列表中更小的权限;
命令:show grants for 用户名;
show grants for ‘cxy’@‘%’;
user表中host列的值的意义
% :匹配所有主机
localhost:localhost不会被解析成IP地址,直接通过
1)information_schema:是一个信息数据库,它保存着关于MySQL服务器所维护的所有其他数据库的>信息;
2)mysql:核心数据库,类似于sql server中的master表,主要负责存储数据库的用户、权限设置、关?>键字等mysql自己需要使用的控制和管理信息;
3)test:测试数据库,没有东西;
语法:create database 数据库名;
或者
create database if not exists 数据库名 default charset utf8 collate utf8_general_ci;
注:默认的数据库编码集:utf8(即UTF-8),collate表示排序规则为utf8_general_ci
语法:show databases;
语法:drop database 数据库名;
1、建表
用户表
create TABLE tb_yh(
user_id VARCHAR(20),
order_time date,
order_category VARCHAR(100),
order_amt FLOAT,
shop_id VARCHAR(20)
)
商户BD表
create table tb_bd(
shop_id VARCHAR(20),
bd_name VARCHAR(100),
bd_team VARCHAR(100),
start_time date,
end_time date
)
2、插数据
用户表
INSERT INTO tb_yh VALUES('A123','2021-01-01 06:50:30','麻辣烫',23.2,'ZL123');
INSERT INTO tb_yh VALUES('A123','2018-01-06 19:20:00','粥',32.2,'SM456');
INSERT INTO tb_yh VALUES('B456','2018-01-15 15:30:00','麻辣烫',22.2,'ZL123');
INSERT INTO tb_yh VALUES('B456','2018-01-25 20:30:00','汉堡',19.2,'HBW123');
INSERT INTO tb_yh VALUES('C789','2018-01-01 22:15:00','小龙虾',60.2,'XM456')
商户BD表
INSERT INTO tb_bd VALUES('ZL123','小明','销售A组','2018-01-01','2018-01-14');
INSERT INTO tb_bd VALUES('ZL123','小张','销售B组','2018-01-15','2099-12-31');
INSERT INTO tb_bd VALUES('SM456','小张','销售B组','2016-01-01','2019-01-01');
INSERT INTO tb_bd VALUES('HBW123','小李','销售C组','2015-01-01','2020-12-31');
INSERT INTO tb_bd VALUES('XM456','小李','销售C组','2015-01-01','2016-01-01')
3、问题
SELECT user_id,SUM(order_amt) from tb_yh GROUP BY user_id HAVING SUM(order_amt)>20 and user_id in(
select user_id from tb_yh where order_time like '%-01-%'
)
运行结果:
SELECT count(*) from (
select user_id from (
select * from tb_yh where month(order_time)=1 and order_category in ('麻辣烫','汉堡')
)a GROUP BY user_id HAVING COUNT(*)=2
)b
运行结果:
SELECT s.shop_id,round(sum(u.ORDER_amt),2) from tb_yh u,tb_bd s where u.shop_id=s.shop_id GROUP BY s.shop_id;
SELECT shop_id,round(sum(ORDER_amt),2) from tb_yh GROUP BY shop_id;