我有以下查询:
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
当我需要选择具有唯一 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 的选择行。
例如:
结果第一个查询(选择 TOP 20 行):
结果第二个查询(选择 NEXT TOP 20 行):
您如何在第二个图像中看到第二个查询具有来自第一个图像的行(结果第一个查询)。
请告诉我哪里错误以及如何写正确?