SQL Server 2008-按FK限制查询
我有以下查询,它对Products表执行全文搜索,并返回1条记录 每个产品都由ShopProducts表引用。每个ShopProduct表示商店中的一个产品,并且具有Products.ProductId的外键。ShopProducts表中的每一行都有一个ShopId列 我的问题是-如何将下面的查询限制为只返回没有@ShopId变量中给定ShopId的ShopProduct的产品SQL Server 2008-按FK限制查询,sql,sql-server,tsql,sql-server-2008,full-text-search,Sql,Sql Server,Tsql,Sql Server 2008,Full Text Search,我有以下查询,它对Products表执行全文搜索,并返回1条记录 每个产品都由ShopProducts表引用。每个ShopProduct表示商店中的一个产品,并且具有Products.ProductId的外键。ShopProducts表中的每一行都有一个ShopId列 我的问题是-如何将下面的查询限制为只返回没有@ShopId变量中给定ShopId的ShopProduct的产品 DECLARE @ShopId uniqueidentifier DECLARE @FullTextQuery nva
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
怎么样
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
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