altertable t1 modify name char(6) //修改字段数据类型 altertable t1 change name NAME char(7);//修改字段名 修改表名
altertable t1 rename T1; //修改表名 增添字段 altertable t1 addd age int,add sex char(1); altertable t1 add age int*first*; //添加到那个位置 altertable t1 add age int after name; //新字段添加到那个字段后 删除字段 altertable t1 drop name;
select*from employ where id >7; select name,salary from employee where post = "teacher" and salary >8000;
select name,salary from employee where salary between20000and30000; select name,salary from employee where salary notbetween20000and30000;
select*from employ where age =73or age =80or age =66; select*from employ where age in(73,80,66);
select*from employee where name like "jin%"; %代表后面任意字符 select*from employee where name like "jin__"; _代表任意一个字符
group by 分组
mysql> set global sqlmode = “ONLYFULLGROUPBY”;#设置过后,只能取分组的字段 分组过后,只能取分组的字段,以及每个组聚合的结果 select post from employee group by post;
没有group by则默认整体算作一组 select max(salary) from employee;
聚合函数:max、min、avg、sum、count
1 2 3 4 5 6 7 8 9
#每个职位有多少个员工 select post ,count(id) as emp_count from employee groupby post; select post ,max(salary) as emp_count from employee groupby post; select post ,min(salary) as emp_count from employee groupby post; select post ,ave(salary) as emp_count from employee groupby post; select post ,sum(age) as emp_count from employee groupby post; #group_concat select post,group_concat(name) from employee groupby post;
HAVING过滤
order by排序
1 2 3
select*from employee orderby age asc;#升序 select*from employee orderby age desc;#降序 select*from employee orderby age asc,id desc;#先按照age升序排,如果age相同则按照id降序排
*语法顺序* select (distinct)字段1,字段2,字段3from 库.表 where 条件 groupby 分组条件 having 过滤 orderby 排序字段 limit n; *执行顺序* from表1、表2->on关系->(左右内)->where->groupby->having->select->distinct->字段->orderby->limit
like
1 2 3 4
select*from employee where name like "jin%"; #正则表达式 select*from employee where name regexp "^jin" ; select*from employee where name regexp "^jin.*(g|n)$" ;#jin开头g、n结尾
多表数据查询
where连表 不建议用。有三种正规的连表方式
三种连表方式:内连接、左连接、右连接、全外连接
内连接:只取两张表的公共部分
1 2
表1innerjoin 表2on 表1.字段 = 表2.字段 select*from employee innerjoin department on employee .dep_id = department .id;
左连接:在内连接的基础上保留左表
1 2
表1leftjoin 表2on 表1.字段 = 表2.字段 select*from employee leftjoin department on employee .dep_id = department .id;
右连接:在内连接的基础上保留右表
1 2
表1rightjoin 表2on 表1.字段 = 表2.字段 select*from employee rightjoin department on employee .dep_id = department .id;
全外连接:在内连接的基础上保留左右两表没有对应关系的记录
1 2 3
表1leftjoin 表2on 表1.字段 = 表2.字段 union 表1rightjoin 表2on 表1.字段 = 表2.字段select*from employee leftjoin department on employee .dep_id = department .id union select*from employee rightjoin department on employee .dep_id = department .id;
8、select完整语法
9、select执行顺序
10、子查询
where exists();是否存在 true false
11、权限管理
1.创建账号
本地帐号
1
createuser'egon1'@'localhost' identified by'123';# mysql -uegon1 -p123
远程账号
1 2 3 4 5
createuser'egon2'@'192.168.31.10' identified by'123';# mysql -uegon2 -p123 -h 服务端ip
createuser'egon3'@'192.168.31.%' identified by'123';# mysql -uegon2 -p123 -h 服务端ip
createuser'egon4'@'%' identified by'123';# mysql -uegon2 -p123 -h 服务端ip