3

嗨,我有这张桌子,

    Recipe = (idR, recipeTitle, prepText, cuisineType, mealType)
    Ingredient = (idI, ingrDesc)
    RecipIngr = (idR*, idI*)

我正在尝试查询一个列表,ingrDesc其中包含有多少接收者的计数ingrDesc。我只想列出那些ingrDesc出现超过 10 次的列表。

这是我所拥有的:

   SELECT a.idI, a.recipeTitle 
   FROM Recipe a
   INNER JOIN recpingr b
   ON a.idr = b.idr
   WHERE a.preptext = '>10'

任何帮助,因为我不知道如何继续此查询

4

2 回答 2

3

将 GROUP BY 与 HAVING 一起使用:

SELECT i.idI, i.ingrDesc, COUNT(*)
FROM Ingredient i
INNER JOIN RecipIngr ri ON i.idI = ri.idI
GROUP BY i.idI, i.ingrDesc
HAVING COUNT(*) > 10
于 2013-04-12T14:46:56.660 回答
1

您需要使用 group by 子句并拥有。我在这里创建了一个快速示例,但我的示例数据没有达到 10,所以我使用了任何多次使用的成分 (> 1)。

这是示例数据:

    create table dbo.recipe (
        idR             int             not null,
        recipeTitle     varchar(100)    not null,
        prepText        varchar(4000)   null,
        cuisineType     varchar(100)    null,
        mealType        varchar(100)    null
    ) 
    go

    insert into dbo.recipe values (1, 'Eggs and Bacon', 'Prep Text 1', 'American', 'Breakfast')
    insert into dbo.recipe values (2, 'Turkey Sandwich', 'Prep Text 2', 'American', 'Lunch')
    insert into dbo.recipe values (3, 'Roast Beef Sandwich', 'Prep Text 3', 'American', 'Lunch')
    go

    create table dbo.ingredient (
        idI             int             not null,
        ingrDesc        varchar(200)    not null
    )
    go

    insert into dbo.ingredient values (1, 'Large Egg')
    insert into dbo.ingredient values (2, 'Bacon');
    insert into dbo.ingredient values (3, 'Butter');
    insert into dbo.ingredient values (4, 'Sliced Turkey');
    insert into dbo.ingredient values (5, 'Lettuce');
    insert into dbo.ingredient values (6, 'Tomato');
    insert into dbo.ingredient values (7, 'Onion');
    insert into dbo.ingredient values (8, 'Bread');
    insert into dbo.ingredient values (9, 'Mustard');
    insert into dbo.ingredient values (10, 'Horseradish');
    insert into dbo.ingredient values (11, 'Sliced Roast Beef');
    go

    create table dbo.recipingr(
        idR             int             not null,
        idI             int             not null
    )
    go

    insert into dbo.recipingr values (1, 1);
    insert into dbo.recipingr values (1, 2);
    insert into dbo.recipingr values (2, 4);
    insert into dbo.recipingr values (2, 5);
    insert into dbo.recipingr values (2, 6);
    insert into dbo.recipingr values (2, 7);
    insert into dbo.recipingr values (2, 8);
    insert into dbo.recipingr values (2, 9);
    insert into dbo.recipingr values (3, 11);
    insert into dbo.recipingr values (3, 10);
    insert into dbo.recipingr values (3, 8);
    insert into dbo.recipingr values (3, 6);
    insert into dbo.recipingr values (3, 5);
    go

这是查询:

    select 
        i.ingrDesc, 
        count(*) ingrCount
    from
        dbo.recipe r
        inner join dbo.recipingr ri on ri.idR = r.idR
        inner join dbo.ingredient i on i.idI = ri.idI
    group by 
        i.ingrDesc
    having
        count(*) > 1
于 2013-04-12T15:00:57.987 回答