数据库_CRUD操作之读取数据


CRUD操作:
create 创建(添加)
read 读取
update 修改

delete 删除

4、读取数据
(1)简单读取,查询所有列(*) 所有行(没有加条件)
select * from Info
(2)读取特定列
select code,name from Info
(3)条件查询
select * from Info where code='p003'
(4)多条件查询
select * from Info where code='p003' or nation='n002' #或的关系
select * from Info where sex=0 and nation='n002' #与的关系
(5)关键字查询(模糊查询)
查所有包含奥迪的汽车
select * from car where name like '%奥迪%'; #百分号%代表任意多个字符
查以'皇冠'开头的所有汽车
select * from car where name like '皇冠%';
查询汽车名称中第二个字符是'马'的
select * from car where name like '_马%'; #下划线_代表任意一个字符
(6)排序查询
select * from car order by powers #默认升序排列
select * from car order by powers desc #升序asc 降序 desc
先按brand升序排,再按照price降序排
select * from car order by brand,price desc

(7)范围查询

select * from car where price > 50 and price<60

select* from car where between 40 and 60

(8)离散查询

select * from where price=30 and price=40 and price=50 and price=60

select * from where price in (30,40,50,60)

select * from where price not in (30,40,50,60)

(9)统计查询(聚合函数)

select count(*) from car            car表里所有的记录

*可以用主键列代替,更快

求和:select sum(price) from car

平均:select avg(price) from car

最大值:select max(price) from car

最小值:select min(price) from car

(10)分页查询

select * from car limit 0,10          跳过0条数据,取10条

规定一个每页显示的条数:m

       当前页数 :          n

select * from car limit (n-1)*m,m

(11)去重查询     ***适合查一列***

select distinct brand from car        ***distinct去重***

(12)分组查询

查询汽车表中,每个系列下汽车的数量

select brand,count(*) from car group by brand

分组完成后,只能取分完组的列

select brand from car group by brand having avg(price) >40

转载于:https://www.cnblogs.com/ysdong/p/5967435.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值