【问题标题】:SQL Query countSQL 查询计数
【发布时间】:2013-04-12 14:44:19
【问题描述】:

你好,我有这张桌子,

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

我正在尝试查询ingrDesc 的列表,并计算ingrDesc 所在的收件人数量。我只想列出出现次数超过10 次的ingrDesc

这是我所拥有的:

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

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

【问题讨论】:

标签: sql sorting join inner-join


【解决方案1】:

将 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

【讨论】:

    【解决方案2】:

    您需要使用 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
    

    【讨论】:

    • 感谢为此付出的努力!谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-01
    • 2019-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多