Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL Server 2008-按FK限制查询_Sql_Sql Server_Tsql_Sql Server 2008_Full Text Search - Fatal编程技术网

SQL Server 2008-按FK限制查询

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

我有以下查询,它对Products表执行全文搜索,并返回1条记录

每个产品都由ShopProducts表引用。每个ShopProduct表示商店中的一个产品,并且具有Products.ProductId的外键。ShopProducts表中的每一行都有一个ShopId列

我的问题是-如何将下面的查询限制为只返回没有@ShopId变量中给定ShopId的ShopProduct的产品

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