简单sql语句

简单的sql语句

语法

select   要查询的字段...   from   表名

作用

查询某个表中的某个或几个字段

select  id, username   from  student  -- 查询student表中的所有人的id和username

查询所有字段 用*

select  *  from  student 
带条件的查询
select   要查询的字段...   from   表名  while 条件  //多个条件可以and

-- 查询所有男同学的名字
select  username   from  student while  sex='男'
-- 查询大于20岁男同学的信息
select  *   from  student while  sex='男'  and age>20
排序

语法

select 字段,字段2...form 表名 [where条件] order by 字段 排序规则

select * from student order by age asc  -- 根据年龄age升序
select * from student order by age desc   -- 根据年龄age降序
查询指定数量limit

语法

select 字段, 字段, … from 表名 limit 开始索引,查询条数

select  * from student limit 1,5  -- 从1 索引开始,查询5条同学表数据,常用语分页查询
select * from student order by age desc limit 0,3 -- 查询年龄最大的3位同学
统计count()

语法

select  count(表主键) from 表名 

例子

select count(id) from student -- 统计学生人数
select count(id) from student where sex='男' -- 统计男同学人数

语法

insert into 表名(字段1,字段2...) values('值1','值2'...)

insert into student(name,age,sex) values("张三",13,'男') -- 添加新同学

语法

update 表名 set 字段=值,字段2=值... [where条件]

update student set age=age+1 -- 所有人年龄加1
update student set username='李思思' where id=3 -- 将id为3的同学名字更改为李思思

语法

delete from 表名 [where 条件]

有条件删除

delete from student where sex='男' -- 删除男学生

无条件删全部

delete from student -- 删除全部学生

删除表

drop table 表名 -- 把整张表毁灭,不留尸体
truncate table 表名 -- 清除表的内容,留下尸体
delete from 表名 -- 一行一行清除表的内容,效率慢,通常带条件删除

删库

drop 库名  -- 数据无价,慎重操作
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值