2013-05-22 68 views
2

我有以下查詢:如何通過不同的過濾結果進行翻頁?

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行): first query

結果第二個查詢(選擇NEXT TOP 20行): second query

你怎麼能在第二看圖像第二個查詢具有來自第一個圖像的行(結果第一個查詢)。

請告訴我,哪裏錯誤,怎麼寫對不對?

+0

嘿改變公司表的別名,然後嘗試 – Strik3r

+0

OFFSET(在MySQL - 限制)只有在SQL Server 2012. – Devart

+0

@Devart沒有意識到這一點。感謝您的信息。因爲我的解決方案是無用的。 – Bryan

回答

1

嘗試這一個 -

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 dbo.Firm f 
WHERE f.blocked = '0' 
    AND (f.name LIKE 'авто%' OR f.phone LIKE 'авто%') 
    AND f.phone + f.[address] != '' 
    AND f.id_city='73041' 
    AND f.dogovor='1' 
ORDER BY f.name 


SELECT * 
FROM (
    SELECT DISTINCT 
     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, 
     row_id = ROW_NUMBER() OVER (ORDER BY f.name) 
    FROM dbo.Firm f 
    WHERE f.blocked = '0' 
     AND (f.name LIKE 'авто%' OR f.phone LIKE 'авто%') 
     AND f.phone + f.[address] != '' 
     AND f.id_city='73041' 
     AND f.dogovor='1' 
) d 
WHERE d.row_id BETWEEN 21 AND 40 
+0

並且不需要'LEFT JOIN價格p'。 – Devart

0

您正在使用哪個歸類?也許這就是爲什麼字符串沒有被你的 '排除的原因'其中f.name不在'子句中

當你正在選擇什麼似乎是表的ID時,你可以不用'Distinct',這個將允許您通過ID過濾前20條記錄。

WHERE f.id_service NOT IN( SELECT TOP 20 f.id_service從公司F WHERE f.blocked = '0' ......

+0

不,我不能使用id,因爲嗨不存在(id_serveice不使用) –

+0

我認爲泰德的建議是第一件要檢查的 - 添加'dogovor = 1'到not in子句中,看看是否有效。 – JamieA

0

首先,它可能是語言不通,但我們很清楚,你的拳頭查詢不會顯示唯一的f.name,而是所有列的唯一組合。

另外,以這種方式使用TOP和DISTINCT是一個非常糟糕的主意。沒有辦法確定你會得到

最後您的NOT IN子句丟失AND f.dogovor='1'