-1
select
    CustomerName,City,State, MatchId,ServingSize, Fruit, DarkGreen, 
    [Red/Orange], Legumes, Starchy, Other, [Grains (oz)], 
    [Whole51%orgreater?], [Meat / Meat Alt], [Fluid Milk (cups)], Calories, 
    [Sodium (MG)], [Saturated Fat (grams)], [Trans Fat (grams)],
    MeetsAllianceForAHealthierGenerationGuidelines, OtherNotes, Name, 
    WebsiteLinkToSource, Date
from
    #FinalTempTable
Where 
    (
        MatchId,ServingSize, Fruit, DarkGreen, [Red/Orange], Legumes, Starchy, 
        Other, [Grains (oz)], [Whole51%orgreater?], [Meat / Meat Alt], [Fluid Milk (cups)],
        Calories, [Sodium (MG)], [Saturated Fat (grams)], [Trans Fat (grams)],
        MeetsAllianceForAHealthierGenerationGuidelines, OtherNotes, Name,
        WebsiteLinkToSource, Date
    )
    IN 
    (
        Select
            MatchId,ServingSize, Fruit, DarkGreen, [Red/Orange], Legumes, 
            Starchy, Other, [Grains (oz)], [Whole51%orgreater?], [Meat / Meat Alt], 
            [Fluid Milk (cups)],Calories, [Sodium (MG)], [Saturated Fat (grams)], 
            [Trans Fat (grams)], MeetsAllianceForAHealthierGenerationGuidelines, 
            OtherNotes, Name, WebsiteLinkToSource, Date
        From
            MealContributions 
        Where
            MatchId IN (Select distinct MatchId from #FinalTempTable)
    )

我正在使用这个查询,但它给出了以下错误:

消息 4145,级别 15,状态 1,第 10 行 在预期条件的上下文中指定的非布尔类型表达式,靠近“,”。

我想要#finaltemptable 中的所有列,并且只有那些MatchId 与#finaltemptable matchid 相同的mealcontributions 列

4

1 回答 1

1

表格

Where 
        (MatchId,ServingSize, Fruit, ... other columns .... ) 
IN 
        (... subquery with columns ...)

适用于 MySQL,但不适用于 SQL Server。

您必须使用列出的所有匹配列组成一个 EXISTS 子句。

 select CustomerName, City, State, MatchId, ServingSize, Fruit, DarkGreen, 
        [Red/Orange], Legumes, Starchy, Other, [Grains (oz)], 
        [Whole51%orgreater?], [Meat / Meat Alt], [Fluid Milk (cups)], Calories, 
        [Sodium (MG)], [Saturated Fat (grams)], [Trans Fat (grams)],
        MeetsAllianceForAHealthierGenerationGuidelines, OtherNotes, Name, 
        WebsiteLinkToSource, Date
   from #FinalTempTable f
  where exists (
        select *
          from MealContributions m
         where f.matchid = m.matchid AND
               f.ServingSize = m.ServingSize AND
               .... all the columns! ...)

最后一个条件

Where MatchId IN (Select distinct MatchId from #FinalTempTable))

是多余的,因为你已经匹配了f.matchid = m.matchid

于 2012-11-21T21:34:36.897 回答