常用的SQL语句

271 阅读2分钟

引言

  • sql语句:数据库的核心语言
  • sql语句对表的添删改查操作:

curd: create(添加) update(修改) read(查询) delete(删除)

查询

在sql语句中,表名和字段加行``,加快sql语句的运行速度

select 字段1,字段2 from 表名
select `name`,`title` from `liuyanban`   查询liyanban表中的name和title字段
select * from `liuyanban`   查询liyanban表中的所有字段 *代表所有
select * from `liuyanban` where `id`=2 查询liyanban表中 id = 2 的数据
select * from `liuyanban` where `id`<>2 查询liyanban表中 id 不等于 2 的数据
select * from `liuyanban` where `id`!=2 查询liyanban表中 id 不等于 2 的数据
select * from `liuyanban` where `id` in(1,4,3)  查询liyanban表中 id的值在()里面 的数据
select * from `liuyanban` where `id` not in(1,4,3)    查询liyanban表中 id的值不在()里面 的数据
select * from `liuyanban` where `id` between 2 and 4   查询liyanban表中 id的值在24区间 的数据
select * from `admin` where `name`='admin' and `pwd`='123' and 并且
select * from `admin` where `name`='admin' or `pwd`='123' or 或者
select * from `liuyanban` order by `time` desc 按照实践降序   desc 降序 asc 升序
select * from `liuynaban` where `id` in(1,4,3) order by `time` desc 如果有where条件,order by 必须放在where后面
select * from `liuyanban` limit 0,3 限制查询的条数 limit 起始位置,查询条数 注意:第一条数据的位置是0
select * from `liuyanban` where `id` in(1,4,3) order by `id` desc limit 0,2 顺序不能乱:where    order by    limit
select count(*) from `liuyanban` 查询表中总的数据条数
select count(*) as allnum from `liuyanban` as取别名
select max(id) from `liuyanban` 查询表中最大的id值 max()最大 min()最小 avg()平均值 sum()求和

添加

insert into 表名 (字段1,字段2,字段3) value(值1,值2,值3)
insert into `admin` (`name`,`pwd`) value('root','123')

修改

update 表名 set 字段1=1,字段2=2,字段3=3 where 条件
update `admin` set `name`='user',`pwd`='456' where `id`=2

删除

delete from 表名 where 条件
delete from `liuyanban` where `id`=4