3

こんにちは、私はこのテーブルを持っています。

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

ingrDescそして、含まれているレシピの数をリストに照会しようとしています。10回以上発生するingrDescものだけをリストしたいのです。ingrDesc

ここに私が持っているものがあります:

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

このクエリを続行する方法がわからないので、助けてください

4

2 に答える 2

1

You need to use a group by clause and having. I have created a quick sample here but my sample data does not go up to 10 so I used any ingredient that was used more than once (> 1).

Here is the sample data:

    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

Here is the query:

    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 に答える