热线电话:13121318867

登录
2019-02-25 阅读量: 675
sql查询语句

问题描述:

订单查询

订单的支付类型:线上支付、线下支付、混合支付

订单的支付状态:未支付、已支付、部分支付

现在需要分页查询所有订单记录,但不包括支付类型为线上支付,且支付状态为未支付的订单

用在分页中,不是一次性查出全部数据!

SQL如何实现?

解决方法:

sql server实现:

约定参数:
page_size:每页的条目数
page:当前页数,从1开始

select top page*page_size * from order as o1
where o1.pay_type != '线上支付' and o1.order_status = '未支付' --过滤条件
and not exist( --分页条件
select top (page-1)*page_size * from order as o2
where o1.pay_type != '线上支付' and o1.order_status = '未支付' --过滤条件
and o1.id = o2.id
);

当前页(N)、显示最大条数(M);这些都是从前台获取的,需要在程序里计算一下,得到结果作为sql的参数。例如,这种情况就是查询第(N-1)M +1到地NM条数据。
用实际数字距离:N=4,M=5;就是要查询地16条到第20条数据。
mysql实现:

select * form order where type <> '线上支付' and status = '未支付' limit 15, 5;

Oracle数据库实现:

select * from (
select rownum as rn * from order where type <> '线上支付' and status = '未支付' and rownum >= 20 ) a where ;
0.0000
3
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子