[英]How to improve SQL query for pagination?
我正在使用数据库和servlet,出现了这样的问题。 我需要从数据库中接收每页6条的数据,为此,我提出了这样的要求
SELECT *, COUNT(*) AS 'count'
FROM product
INNER JOIN product_category
on product.product_category_id = product_category.id
INNER JOIN company_manufacturer_product
on product.company_manufacturer_product_id =
company_manufacturer_product.id
GROUP BY 1 LIMIT 6 OFFSET 0;
其中6是每页的最大项目数,0是页数乘以最大商品数量。 但是通过第二页上的这种实现,我有重复的产品我该如何改进它?
我形成请求的代码部分:
StringBuilder startResponse = new StringBuilder("SELECT *, COUNT(*) AS 'count' FROM product " +
"INNER JOIN product_category on product.product_category_id = product_category.id " +
"INNER JOIN company_manufacturer_product on product.company_manufacturer_product_id=company_manufacturer_product.id");
if (nonNull(form.getProductMax()) && nonNull(form.getPage())) {
startResponse.append(" LIMIT ").append(form.getProductMax()).append(" OFFSET ").append(form.getPage() * form.getProductMax());
}
我的数据库在没有LIMIT和OFFSET的情况下响应:
当我使用上述查询时,我的数据库将响应,当我转到商品的第一页时,此请求将发送到数据库:
当我转到商品的第二页时,我将这样的请求发送到数据库
SELECT * , COUNT(*) AS 'count'
FROM product
INNER JOIN product_category
on product.product_category_id = product_category.id
INNER JOIN company_manufacturer_product
on product.company_manufacturer_product_id =
company_manufacturer_product.id
GROUP BY 1 LIMIT 6 OFFSET 6;
我有这样的回应:
我不明白是什么问题。 我必须使用COUNT个请求! 如何证明呢?
不反对这个问题的解决方案,根据上述方法,对原始sql添加order by
即可解决问题。 但是我认为我有更好的分页习惯:使用has_more
, last_product_id
和limit_num
类的参数将客户端与服务器连接。
has_more
指示服务器中是否剩余数据; last_product_id
表示最后响应数据的id; limit_num
表示每页的数量。
因此,客户端可以使用has_more
来确定是否发送请求,如果是,则客户端向服务器发送带有last_product_id
和limit_num
的请求; 对于服务器,sql可以是这样的:
select * from table where id < $last_product_id order by id desc
limit $limit_num + 1; =>$datas
并且,count($ datas)和$ limit_num可以计算has_more
和last_product_id
的值:
$has_more = 0;
$data_num = count($datas);
if ($data_num > $page_limit) {
$has_more = 1;
array_pop($datas);
$data_num--;
}
$last_product_id = end($datas)['id'] ?? 0;
SELECT *,COUNT(product.id)作为'count',来自产品INNER JOIN在product.product_category_id上的product_category = product_category.id INNER JOIN company_manufacturer_product在product.company_manufacturer_product_id = company_manufacturer_product.id上按product.id分组按product.id进行订购SET LIMIT 6 OFF 0;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.