【问题标题】:Where clause for undefined columns未定义列的 Where 子句
【发布时间】:2023-03-27 14:37:01
【问题描述】:

我正在编写查询,但遇到了问题。

select name, address, count(BL.card_no) 
from Book_Loans BL 
inner join Borrower B on BL.card_no = B.card_no 
where count(BL.card_no) > 1 group by name;

这不起作用,因为我不能使用“where count(BL.card_no) > 1”,因为它显示“无效使用组函数”。但是我需要确保只显示大于 1 的卡号计数,我还能怎么做?

【问题讨论】:

    标签: mysql where-clause


    【解决方案1】:

    您必须将谓词放在HAVING 子句中:

    select name, address, count(BL.card_no) 
    from Book_Loans BL 
    inner join Borrower B on BL.card_no = B.card_no 
    group by name
    having count(BL.card_no) > 1;
    

    【讨论】:

      【解决方案2】:

      对于聚合函数,使用 have 而不是 where

      select name, address, count(BL.card_no) 
      from Book_Loans BL 
      inner join Borrower B on BL.card_no = B.card_no 
      group by name
      having  count(BL.card_no) > 1;
      

      【讨论】:

        猜你喜欢
        • 2021-05-27
        • 2016-07-11
        • 2017-08-12
        • 2011-06-03
        • 1970-01-01
        • 2019-09-03
        • 1970-01-01
        • 2021-09-19
        相关资源
        最近更新 更多