一篇文章带你轻松学会数据库语句

插入

1.给指定字段添加数据

INSERT INTO 表名(字段1,字段2...) VALUES (值1,值2...) 

 例如:insert into Student(id, name) values(1,'张三');


2.给全部字段添加数据

INSERT INTO 表名 VALUES (值1,值2...);

insert into Student values (1,'张三'); 


3.批量添加数据

​
INSERT INTO 表名(字段名1,字段名2...) VALUES (值1,值2...),(值1,值2...),(值1,值2...);

​

insert into Student values (1,'张三'),(2,'李四'),(3,'王五');  


修改

修改某字段的值

UPDATE 表名 SET 字段名1 = 值1, 字段名2 = 值2... [where 条件] 

例如:修改id = 1的name字段为小明

UPDATE Student set name = '小明' where id = 1;

修改全部

update Student set name = '小明' ;

删除

delect from 表名 where 字段1 = 值1,字段2 = 值2...;


查询 select

select 字段 

from 表名 

where 条件  

group by 分组字段 

having 分组后条件列表

order by 排序字段列表

limit 分页参数

执行顺序 from -> where -> group by having ->select  -> order by -> limit

函数

count  统计数量 (不计算null值)       max 最大值        min 最小值        avg 平均值        sum 求和

语法:  select 函数(字段) from 表名;

分组查询

select 字段 from 表名 [where 条件] group by 分组字段名 [having 分组之后条件]

将表中按性别分组

select gender, count(*) from 表名 group by gender;

例如:查询年龄大于30,按照部门分组,获取部门的员工数量大于100的部门

select department, count(*) from 表名 where age > 30 group by department having count(*) > 100;

排序查询

语法:select 字段 from 表名  order by 字段1 排序方式1, 字段2 排序方式2;

排序方式      ASC: 升序  DESC: 降序

例如:先按照年龄升序排序,年龄相同,再按照入职时间降序排序

select * from emp order by age asc , entrydate desc;

分页查询

语法:select 字段 from 表名 limit 起始索引,查询记录数;

例1:查询所有员工小于等于35的员工的姓名和年龄,并按照年龄升序,年龄相同按入职降序排序

select name, age from student where age <= 35 order by age asc, entrydate desc;

例2:查询性别为男,年龄在20-40以内的前五名员工, 对查询的结果按年龄降序排序,年龄相同按入职时间升序排序

select * from emp where gender = '男' and age between 20 and 40 order by age asc, entrydate asc limit 5;

DCL语句

查询用户:select * from user

创建用户:create user ‘用户名’@‘主机名’ identfied  by ‘密码’

修改用户: alter user ‘用户名’@‘主机名’ identified with mysql_password  '新密码'

删除用户:drop user ‘用户名’@‘主机名’

控制权限

权限:

ALL,ALL privileges 所有权限        select 查询数据        insert 插入数据         update 修改数据         

delete 删除数据         alter 修改表        dorp删除数据/表/视图        create 创建数据库/表

查询该表的权限:show grants for '用户名'@'主机名';

授予权限:grant 权限名 on 数据库.表名 to '用户名'@'主机名';

撤销权限:revoke 权限 on 数据库.表名 from '用户名'@'主机名';

函数

字符串函数

字符串拼接 concats(s1,s2,...)

lower(string) 将字符串变成小写 

upper(string) 变大小

lpad (str, n, pad) 左填充 str为字符串 n为长度 pad为填充的字符

rpad(str, n, pad)右填充      rpad('123',6,‘0’) -> 000123

trim(string) 去掉字符串首尾的空格        trim(' hello mysql '); ->hello mysql

substring(str, start, len) 从第start位置起,到len的长度结束的字符串         

substring('hello mysql',1,5); ->hello

例:将学生的学号,都改为6位数,不足的在前面补0。学生1的学号就为000001

update 表名 set studentID = lpad(studentID,6,'0')

数值函数

ceil(x)        向上取整

floor(x)        向下取整

mod(x,y)        返回x/y的模        select mod(5, 2);   结果为1

rand()         返回0 - 1之间的随机数

round(x, y)        求参数x四舍五入,并且保留y位小数

例:写出随机6位数: select lpad( round ( rand() * 1000000 , 0) , 6 , '0');

日期函数

curdate()        返回当前日期

curtime()        返回当前时间

now()             返回当前时间和日期

yea(date)       返回date的年份

month(date)  返回date的月份

day(date)       返回date的日期

date_add(date, interval expr type)        返回一个日期/时间加上 间隔时间expr后的时间值

datediff(date1, date2)        返回起始时间date1和结束时间date2之间的天数

流程函数

if(value, a, b)        value为true返回a,否则返回b

ifnull(a, b)        第一个值为null返回b,不为null返回a

case when [value] then [result] ... else[default] end        如果value为true返回reslut  否则返回default的默认值

case 列名 when [val1] then [result] ... else [default1] end        如果等于val1则返回result1否则返回default

例如:查找学生姓名及其选课的结果如果选1/2号则为体育活动 其他为艺术课

select name, (case course_id when '1' then '体育' when '2' then '体育' else '艺术' end) from 表名

学生有三门课的成绩分别为语数英,60分以上为及格,80分以上为优秀

select name

(case when math >= 80 then '优秀' then math >= 60 then '及格' else '不及格' end) '数学',

(case when english>= 80 then '优秀' then english>= 60 then '及格' else '不及格' end) '英语',

(case when chinese>= 80 then '优秀' then chinese>= 60 then '及格' else '不及格' end) '语文',

form 表名

约束

非空        not null

唯一        unique

主键        primary key

默认约束        default

检测        check        (保证字段值满足某一条件)

外键        foring  key        用于让两张表的数据之间建立连接,保证数据的一致性和完整性

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值