我有以下查詢:如何通過不同的過濾結果進行翻頁?
SELECT DISTINCT TOP 20
f.id_service as f_id_service,
f.id_city as f_id_city,
f.name as f_name,
f.address as f_address,
f.business as f_business,
f.web as f_web,
f.phone as f_phone,
f.id_firm as f_id_firm
FROM
Firm f
left join
Price p ON p.id_service = f.id_service
AND p.id_city = f.id_city
AND p.id_firm = f.id_firm
WHERE
f.blocked = '0'
AND (f.name LIKE 'авто%'
OR f.phone LIKE 'авто%')
AND (f.phone != '' OR f.address != '')
AND f.id_city = '73041'
AND f.dogovor = '1'
ORDER BY f.name ASC
該查詢顯示TOP 20行具有獨特f.name
當我需要選擇下一個20行具有獨特f.name我使用一個查詢:
SELECT DISTINCT TOP 20
f.id_service AS f_id_service,
f.id_city AS f_id_city,
f.name AS f_name,
f.address AS f_address,
f.business AS f_business,
f.web AS f_web,
f.phone AS f_phone,
f.id_firm AS f_id_firm
FROM
Firm f
LEFT JOIN
Price p ON p.id_service = f.id_service
AND p.id_city = f.id_city
AND p.id_firm = f.id_firm
WHERE
f.name NOT IN (SELECT DISTINCT TOP 20
f.name
FROM
Firm f
WHERE
f.blocked = '0'
AND (f.name LIKE '????%'
OR f.phone LIKE '????%')
AND (f.phone != '' OR f.address != '')
AND f.id_city = '73041'
ORDER BY f.name ASC)
AND f.dogovor = '1'
AND f.blocked = '0'
AND (f.name LIKE '????%'
OR f.phone LIKE '????%')
AND (f.phone != '' OR f.address != '')
AND f.id_city = '73041'
AND f.dogovor = '1'
ORDER BY f.name ASC
但我看到在最後一個查詢中,我查詢了從第一個查詢中選擇具有唯一f.name的行。
例如:
結果第一查詢(選擇TOP 20行):
結果第二個查詢(選擇NEXT TOP 20行):
你怎麼能在第二看圖像第二個查詢具有來自第一個圖像的行(結果第一個查詢)。
請告訴我,哪裏錯誤,怎麼寫對不對?
嘿改變公司表的別名,然後嘗試 – Strik3r
OFFSET(在MySQL - 限制)只有在SQL Server 2012. – Devart
@Devart沒有意識到這一點。感謝您的信息。因爲我的解決方案是無用的。 – Bryan