次のクエリがあります。
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
このクエリは、一意の上位 20 行を表示しますf.name
一意の f.name を持つ次の 20 行を選択する必要がある場合は、次のクエリを使用します。
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 を持つ行を選択してクエリを実行していることがわかります。
例えば:
結果の最初のクエリ (上位 20 行を選択):
結果の 2 番目のクエリ (NEXT TOP 20 行を選択):
2 番目の画像でどのように確認できるか 2 番目のクエリには、最初の画像 (最初のクエリの結果) からの行があります。
エラーの場所と正しい書き方を教えてください。