1. 当前位置:网站首页 > Mysql

mysql常用方法


mysql的sql语句

普通CURD

增加:
insert into 表名 values(列的值,列的值.....);  【自增id、有默认值、非必须值可以不填写】(自增id可以采用雪花算法生成,解决特定场合问题)
insert into 表名 values(列的值,列的值.....),(列的值,列的值.....);【批量插入】
删除:
delete from 表名 where 条件; 【建议当没条件时输入 where 1=1 判断】
修改:
update 表名 set 列名=值,列名=值 ... where 条件; 【如果省略了条件就是所有的条件都修改】
查询:
select * from 表名;
select 列名,列名 from 表名;
select 列名,列名 from 表名 where 条件 and 条件;

高级查询

数学运算
select name, math+english from grades; 【查询学生的数学+英语总成绩】

去重
select distinct 列名 from 表名;
select distinct 列名,列名 from 表名; 【多列同时满足重复】
select *,distinct 列名 from 表名; 【列出满足去重条件的所有记录信息】

排序
select 列名,列名... from 表名 order by 列名 asc / desc; 【acs/desc(升序/降序)默认升序】
select 列名,列名... from 表名 order by 列名,列名 asc / desc; 【多个条件时可以直接追加列在后面】

查询条件
>、 >=、 <、 <=、 =、 <=>【这个和=差别在于判断null = null时不同,这个为真,=为假】
between a and b 【a和b的区间】
in (option) 【枚举条件】
is null 和 is not null
like
and或&&
or或||
not或!

like高级用法,长度模糊匹配
select * from student where name like '张__'; 【两个下划线代表匹配三位,及张宇和张周磊,只会找到张周磊】

分页
select * from 表名 limit 3
偏移
select * from 表名 limit 3 offset 3;  【例如前面三个值主键为1、2、3,这样写就会匹配到4、5、6,平常不写默认为offset 0】

计算数量
select count(id) from 表名
求和
select sum(列名或者表达式) from 表名;
select sum(chinese), sum(chinese+ math) from 表名;
其他
avg()平均值
max() 最大值
min() 最小值

分组
select name from 表名 group by 列名;  【根据列名分组】
select role, max(salary), min(salary) from 表名 group by role; 【先根据角色分组,分别统计不同橘色最大和最少工资】
select role, max(salary), min(salary) from 表名 where role!='程序员' group by role; 【where优先于分组前】
select role, max(salary), min(salary) from 表名 group by role having min(salary) > 1000; 【having于分组后】

联合查询
select * from student as a, class as b where a.class_id = b.class_id;
select a.name, b.class_name from student as a, class as b where a.class_id = b.class_id;

多表查询
select 字段 from 表1 别名 [inner] join 表2 别名2 on 连接条件 and 其他条件

内连接(join on 既可以是内连接也可以是外连接, 而where只能是内连接)
select 字段 from 表名1 inner join 表名2 on 连接条件 and 其他条件

外连接(外连接分为左外连接和右外连接)
select 字段 from 表名1 left join 表名2 on 连接条件;  【第一张为主】
select 字段 from 表名1 right join 表名2 on 连接条件;  【第二张为主】

自连接
select 字段 from 表名1 as s1, 表名1 as s2 where s1.id = s2.id and s1.score < s2.score and s1.course_id =1 and s2.couser_id = 2;  【显示不同学科,course_id =2比course_id =1高的信息】

单行子查询
select 字段 from 表名 as s1,表名1 as s2 where s1.name= 'test' and s1.class_id = s2.class_id
多行子查询
select 字段 from 表名 where course_id in (select id from course where name = 'chinese' or name = 'math')

合并查询
union 【会去重】
union all【不会去重】
select 字段 from course where name = '英文' union select  * from course where id < 3;  == select 字段 from course where name = '英文 or id <3;

基础命令

创建数据库:
create database 数据库名;

查看所有数据库
show databases;

选中数据库:
use 数据库名;

删除数据库:
drop database 数据库名;

创建表:
create table 表名 (列名 类型, name varchar(20), age int not null default '18');
create table 表名 (列名 类型, id int unique); 【id为唯一值】
create table 表名 (列名 类型, id int primary key auto_increment); 【id为主键,自增】
create table 表名 (列名 类型, age int foreign key ); 【age为外键】

查看所有表:
show tables;

查看表结构:
desc 表名;

删除表:
drop table 表名;

权限控制(创建一个数据库用户test,具备对saklia数据中所有表的select/insert权限;):
grant select,insert on sakila.* to 'test'@'localhost' identified by 'test123456';
flush privileges;
收回insert权限
revike insert on sakila.* from 'test'@'localhost'
设置root密码
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'liu123456';
设置允许远程访问(5.8)
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'liu12345' WITH GRANT OPTION;
设置允许远程访问(指定ip)
GRANT ALL PRIVILEGES ON *.* TO 'root'@'221.226.250.19' IDENTIFIED BY 'liu12345' WITH GRANT OPTION;

表结构修改

新增一列:
alter table 表名 add column 字段 类型 属性;
alter table test add column addr varchar(50) not null;
alter table test add column addr varchar(50) not null after user1; 【插入到指定列位置,这里是查到到字段user1后面,默认不写添加到最后一列】
alter table test add column addr varchar(50) not null first;【插入到第一个】

本文最后更新于2020-11-19,已超过 3个月没有更新,如果文章内容或图片资源失效,请留言反馈,我们会及时处理,谢谢!
版权说明

本文地址:http://www.liuyangdeboke.cn/?post=15
未标注转载均为本站远程,转载请注明文章出处:

发表评论

联系我们

在线咨询:点击这里给我发消息

微信号:17721538135

工作日:9:00-23:00,节假日休息

扫码关注