第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何改進分頁的 SQL 查詢?

如何改進分頁的 SQL 查詢?

牛魔王的故事 2021-11-17 10:20:13
我正在處理數(shù)據(jù)庫和 servlet,出現(xiàn)了這樣的問題。我需要從每頁 6 條的數(shù)據(jù)庫中接收數(shù)據(jù),為此我提出了這樣的請求SELECT *, COUNT(*) AS 'count' FROM productINNER 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.idGROUP BY 1 LIMIT 6 OFFSET 0;其中 6 是每頁的最大項目數(shù),0 是頁碼乘以最大貨物數(shù)量。但是在第二頁上有這樣的實現(xiàn)我有重復的產(chǎn)品我該如何改進它?我形成請求的代碼部分: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());        }我的數(shù)據(jù)庫沒有 LIMIT 和 OFFSET 響應:當我使用上述查詢時,我的數(shù)據(jù)庫響應,當我轉(zhuǎn)到帶有商品的第一頁時,此請求將發(fā)送到數(shù)據(jù)庫:當我翻到有商品的第二頁時,我向數(shù)據(jù)庫發(fā)送了這樣一個請求SELECT * , COUNT(*) AS 'count' FROM product INNER JOIN product_category  on product.product_category_id = product_category.idINNER JOIN company_manufacturer_product   on product.company_manufacturer_product_id =      company_manufacturer_product.idGROUP BY 1 LIMIT 6 OFFSET 6;我有這樣的回應:我不明白是什么問題。我必須通過 COUNT 使用請求!怎么證明?
查看完整描述

2 回答

?
紅糖糍粑

TA貢獻1815條經(jīng)驗 獲得超6個贊

不反對這個問題的解決方法,按照上面的方法,order by在原來的sql中添加就可以解決問題。但是我想我有分頁一個更好的做法:使用參數(shù)一樣has_more,last_product_id并limit_num與服務器連接的客戶端。


has_more指示服務器中是否有更多數(shù)據(jù); last_product_id表示上次響應數(shù)據(jù)的id; limit_num表示每頁的數(shù)量。


這樣,客戶端可以使用has_more,以確定發(fā)送請求或沒有,如果是,則客戶機發(fā)送請求與last_product_id和limit_num給服務器; 對于服務器,sql 可以是這樣的:


select * from table where id < $last_product_id order by id desc

limit $limit_num + 1; =>$datas

而且,計數(shù)($ 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; 


查看完整回答
反對 回復 2021-11-17
?
桃花長相依

TA貢獻1860條經(jīng)驗 獲得超8個贊

SELECT *, COUNT(product.id) 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 product.id OFF SET 6 0;

查看完整回答
反對 回復 2021-11-17
  • 2 回答
  • 0 關注
  • 161 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號