2

良い一日。

データベース内のテーブルから個別の行を選択するクエリ:

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, 
p.name as p_name 
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

ナビゲーションにはNOT INを使用します:

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, 
p.name as p_name 
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 
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 p.id_city='73041' AND p.include='1' AND p.blocked='0' AND f.blocked='0' AND p.id_group='44' AND p.id_subgroup='266' 
ORDER BY f.name ASC
) 
AND p.id_firm=f.id_firm AND p.id_city='73041' AND p.include='1' AND p.blocked='0' 
AND f.blocked='0' AND p.id_group='44' AND p.id_subgroup='266'
ORDER BY f.name ASC

最後のクエリはチェックf.name NOT INでしたが、最初のクエリのように文字列全体をチェックする必要がありますf.name

これはどうやって作るの?

4

2 に答える 2

2

これはあなたに役立つ可能性があります-

;WITH cte AS 
(
    SELECT DISTINCT 
          f_id_service = f.id_service
        , f_id_city = f.id_city
        , f_name = f.name
        , f_address = f.[address]
        , f_business = f.business
        , f_web = f.web
        , f_phone = f.phone
        , f_id_firm = f.id_firm
        , p_name = p.name
    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 p.id_city = '73041' 
        AND p.[include] = '1' 
        AND p.blocked = '0' 
        AND f.blocked = '0' 
        AND p.id_group = '44' 
        AND p.id_subgroup = '266'
)
SELECT TOP 20   
      f_id_service
    , f_id_city
    , f_name
    , f_address
    , f_business
    , f_web
    , f_phone
    , f_id_firm
    , p_name
FROM cte
WHERE f_name NOT IN (
        SELECT DISTINCT TOP 20 f_name
        FROM cte
        ORDER BY f_name
    )
ORDER BY f_name
于 2013-04-23T13:36:02.520 に答える