sql常用语句

1、增加

insert into tableName values(val1,val2,val3) //必须与数据库对应
insert into tableName(columnName1,columnName2) values(val1,val) 

2、删除

delete from tableName where columnName=value
delete from tableName where columnName in (%s)

3、修改

update tableName set columnName=value

4、基本查询

//查询所有数据
select * from tableName
//查询某列数据
select column1,column2 from tableName

5、条件查询

//查询大于某字段的数据(时间为例)小于某字段范围内 这里的date为字段名
select * from tableName where date>='2017-3-5' and date<'2018-3-2'
//模糊查询 addre为字段名 查询所有上海的
select * from tableName where addre like '上海%'
//查询男或者女 哈哈,举例
select * from tableName where sex='男' or sex='女'
//查询符合条件的
select * from tableName where name in ('张三','李四','王五')

6、排序

//通过某字段 升序 排序 ps:id为字段名
select * from tableName order by id asc
//通过某字段 降序 排序
select * from tableName order by id desc

7、分组

select * from tablesName group by columnName
//对查询的结果分组 ps:id和sex都是字段
select * from tableName where id>1 group by sex
//having条件查询
select * from tableName where id>1 group by sex having age>12

   注意: where跟在from语句之后使用,对查询到的原始数据筛选

              having跟在group by语句之后,对分组后的数据进行筛选,否则和wher语句一样

8、关联查询

select column1,column2 from table1,table2 where table1.id=table2.id

9、分页查询

select * from tableName limit x,y
//x:(当前页数-1)*行数
//y:行数

10、函数

//和
select sum(columnName) from tableName
//平均
select avg(columnName) from tableName
//个数
select count(*) from tableName
//注意:不计算null值
//截取查询
select substr(column,x,y) from tableName
//x:起始位置 注意 起始不是0,是1
//y:结束位置

11、为表动态添加字段

//在id字段后添加一条字段
alter table tableName add columnName varchar(255) after id

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值