Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何改进分页的SQL查询?_Java_Mysql_Servlets - Fatal编程技术网

Java 如何改进分页的SQL查询?

Java 如何改进分页的SQL查询?,java,mysql,servlets,Java,Mysql,Servlets,我正在处理数据库和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_manu

我正在处理数据库和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());
        }
我的数据库响应没有限制和偏移:

My database respone当我使用上述查询时,当我转到商品的第一页时,此请求将发送到数据库:

当我翻开第二页查看商品时,我会向数据库发送这样的请求

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;
我的回答是:

我不明白是什么问题。我必须通过计数使用请求!如何证明这一点

从产品内部联接中选择*,计数(product.id)为“计数” product.product\u category\u id=product\u category.id上的product\u category 内部连接公司\制造商\产品 product.company\u manufacturer\u product\u id=company\u manufacturer\u product.id 按产品分组。id按产品下单。id限制6偏移0


不反对这个问题的解决,按照上面的方法,在原始sql中添加
orderby
,可以解决这个问题。 但我认为我有一个更好的分页实践:使用参数,如
has\u more
last\u product\u id
limit\u num
来连接客户端和服务器

has_more
表示服务器中是否有更多数据;
last\u product\u id
表示上次响应数据的id;
limit\u num
表示每页的页数

因此,客户端可以使用
has\u more
来确定是否发送请求,如果发送请求,则客户端向服务器发送带有
last\u product\u id
limit\u num
的请求;对于服务器,sql可以是这样的:

select * from table where id < $last_product_id order by id desc
limit $limit_num + 1; =>$datas

也许可以尝试将ORDERBY子句添加到查询中。顺便说一句,如果“id”是您的主键,则
GROUP By 1
没有任何用处,
COUNT(*)
将始终为1。是的,谢谢您的帮助,没有
按顺序进行分页是没有任何意义的。只要您在同一查询中使用
*
和GROUP By,您就已经输了。如果是我,我会重新开始。获取一个正常工作的查询,然后查看数据集的可能大小,然后讨论分页
$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;