mysql常用语句

本篇讲述了,sql常用的一些增删改查语句

MySQL

MySQL是一个关系型数据库管理系统,是最流行的关系型数据库管理系统之一。MySQL所使用的 SQL 语言是用于访问数据库的最常用标准化语言。由于其社区版的性能卓越,搭配 PHP 和 Apache 或者 node 可组成良好的开发环境

查询

全部查询

1
select * from user

查询名字为user的表的所有参数

条件查询

1
select name from user where id=1

查询名字为user的表中满足条件为id为1的数据的name值

1
select * from user where id=1

查询名字为user的表中满足条件为id为1的所有参数

多个条件查询

1
select * from user where id=1 and age=2

查询名字为name的表中满足条件为id为1并且年龄为2的数据的所有参数

删除

1
delete from user where id=1

在user表中删除满足id为1的那一条数据

修改

1
update user set name = ZXX where id = 1;

在user表中把id为1的那条数据的name值修改为 ZXX

如果你需要一次修改多个参数,那么


1
update user set name = ZXX,age = 20 where id = 1;

注意:如果你的参数名称为系统变量的话。。请加上 ``。不然会报错

1
update user set `name` = ZXX,`age` = 20 where id = 1;

新增

1
insert into user (name,age) values (zxx,16)

在user表中新增一条name为zxx,age为16的数据

比较复杂的一些

多表联查

1
select a.name,a.age,b.size,c.title,c.content from a join b on a.id = b.cid join c on a.id = c.pid where a.age = 16 and b.size = 8 limit 10,20

从a,b,c三个表中。查出a.name,a.age,b.size,c.title,c.content 条件是a.id =b.cid =c.pid。条件是a.age等于16. b.size等于8 从第10条开始取20条

一些条件判断语句

都是通过某个表的某一个参数来判断的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
大于 -> >
小于 -><
等于 -> =
大于等于 -> >=
小于等于 -> <=
包含 -> like
不包含 -> not like
开始 以 -> like 9%
结束 以 -> like %9
是null -> is null
不是null -> is not null
介于1-9之间 -> between 1 and 9
不介于1-9之间 -> not between 1 and 9
在列表中 -> in ('1','2')
不在列表中 -> not in ('1','2')

我们平常所见的分页就是通过 <–limit 0,20,limit 1,20–> 来实现的

结束

以上,为常用的一些sql语句。会不定期更新的

本文总阅读量

坚持原创技术分享,您的支持将鼓励我继续创作!