2009-09-20 5 views
1

J'ai la requête suivante, qui effectue une recherche de texte intégral (CONTAINSTABLE) sur la table Produits et renvoie 1 enregistrement.SQL Server 2008 - limitation de la requête par FK

Chaque produit est référencé par les ShopProducts tableau (chaque ShopProduct représente un produit dans un magasin et est a une clé étrangère à Products.ProductId. Chaque ligne de la ShopProducts table a une ShopId colonne

Ma question est -. comment puis-je limiter la requête ci-dessous pour retourner uniquement Produits que ne pas ont un ShopProduct pour le ShopId donné dans la variable @ShopId?

DECLARE @ShopId uniqueidentifier 
DECLARE @FullTextQuery nvarchar(1000) 

SET @ShopId = 'a7e7d519-27f0-4d95-a1dd-87d992a0478c' 

SET @FullTextQuery = 'ISABOUT("*Palmolive*","*Naturals*","*Shower*","*Milk*","*Nourishing*","*With*","*Honey*")' 


SELECT TOP 1 
     ftt.RANK, 
     p.ProductId, 
     p.SearchableDescription 
FROM Products p 
JOIN CONTAINSTABLE(Products, 
        SearchableDescription, 
        @FullTextQuery) AS ftt ON ftt.key = p.ProductId 
ORDER BY ftt.RANK DESC 

Répondre

0
SELECT TOP 1 
ftt.RANK, p.ProductId, p.SearchableDescription 
FROM Products p 
INNER JOIN CONTAINSTABLE(Products, SearchableDescription, @FullTextQuery) AS ftt 
ON ftt.[KEY]=p.ProductId 
LEFT OUTER JOIN ShopProduct s 
ON (p.ProductId = s.ProductId AND s.ShopId = @ShopId) 
WHERE s.ProductId IS NULL 
ORDER BY ftt.RANK DESC 
0

Que diriez-vous

WITH ProductsExcept(ProductId,SearchableDescription) as (
    SELECT ProductId, SearchableDescription 
    FROM Products p 
    WHERE NOT EXISTS (
    SELECT * FROM ShopProducts 
    WHERE ShopProducts = ProductID 
    AND ShopID <> @ShopId 
) 
) 
    SELECT 
    ftt.RANK, 
    p.ProductId, 
    p.SearchableDescription 
FROM ProductsExcept p 
JOIN CONTAINSTABLE(Products, 
        SearchableDescription, 
        @FullTextQuery) AS ftt ON ftt.key = p.ProductId 
ORDER BY ftt.RANK DESC 
Questions connexes