【问题标题】:SQL select from 1 x N where all bigger thanSQL select from 1 x N where all大于
【发布时间】:2019-11-25 13:19:33
【问题描述】:

我有表 booksbookType 构成 1 X n 关系。

books

+-----+------------------+----------+-------+
| id  |      title       | bookType | price |
+-----+------------------+----------+-------+
|   1 | Wizard of Oz     |        3 |    14 |
|   2 | Huckleberry Finn |        1 |    16 |
|   3 | Harry Potter     |        2 |    25 |
|   4 | Moby Dick        |        2 |    11 |
+-----+------------------+----------+-------+

bookTypes

+-----+----------+
| id  |   name   |
+-----+----------+
|   1 | Fiction  |
|   2 | Drama    |
|   3 | Children |
+-----+----------+

我将如何检索所有书籍都比例如更昂贵的书籍类型12(美元)? 在这种情况下,预期的输出将是:

+-----+----------+
| id  |   name   |
+-----+----------+
|   1 | Fiction  |
|   3 | Children |
+-----+----------+

【问题讨论】:

    标签: mysql sql mariadb


    【解决方案1】:

    你可以使用not exists:

    select t.*
    from bookTypes t
    where not exists (
        select 1
        from books b
        where b.bookType = t.id and b.price < 12
    )
    

    如果您想选择至少有一本相关书籍的书籍类型:

    select t.*
    from bookTypes t
    where 
        exists (select 1 from books b where b.bookType = t.id)
        and not exists (select 1 from books b where b.bookType = t.id and b.price < 12)
    

    【讨论】:

      【解决方案2】:

      您可以直接考虑使用having min(price) &gt;= 12bookType 分组

      select t.id, t.name
        from bookTypes t
        join books b
          on t.id = b.bookType
       group by b.bookType   
       having min(price) >= 12
      

      此外,如果您的数据库版本至少为10.2,那么您还可以使用一些window functions 进行分析查询,例如min(..) over (partition by .. order by ..)

      with t as
      (
      select t.id, t.name, min(price) over (partition by bookType) as price
        from bookTypes t
        join books b
          on t.id = b.bookType
      )
      select id, name 
        from t
       where price >= 12 
      

      其中min() over (..) 窗口函数通过使用partition by bookType 确定每种书型的最低价格

      Demo

      【讨论】:

        【解决方案3】:

        执行GROUP BY,使用HAVING 仅返回最低价格> 12 的书型。

        SELECT bt.name
        FROM bookTypes bt
        INNER JOIN books b ON b.bookType = bt.id
        group by bt.name
        HAVING SUM(b.price <= 12) = 0;
        

        【讨论】:

        • 我认为你需要计算价格小于或等于12的书籍数量为零,即它们没有出现。
        【解决方案4】:

        我认为 GMB 的解决方案可能是迄今为止最好的。但为了完整起见:您还可以将ALL 运算符与相关子查询一起使用。这可能是最直接的解决方案。

        SELECT *
               FROM booktypes bt
               WHERE 12 < ALL (SELECT b.price
                                      FROM books b
                                      WHERE b.booktype = bt.id);
        

        【讨论】:

        • 这也是一个不错的解决方案 +1。
        【解决方案5】:

        你能不能只从书籍内部选择 id WHERE price > 12 的 bookTypes ?

        SELECT bt.*
        FROM bookTypes bt
        INNER JOIN books b ON b.bookType = bt.id
        WHERE b.price > 12
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-10-07
          • 1970-01-01
          • 2018-02-20
          • 1970-01-01
          • 2016-09-18
          • 2010-11-03
          • 1970-01-01
          相关资源
          最近更新 更多