【问题标题】:How to include COUNT function in where clause?如何在 where 子句中包含 COUNT 函数?
【发布时间】:2022-01-17 20:45:26
【问题描述】:

我有一个客户记录表。我正在尝试仅获取自特定日期以来访问过 10 次以上的客户的数据。这是我现在得到的代码。任何故障排除帮助将不胜感激!

    Select 
         customer_id,
         order_id,
         order_date
    from Table1
    where order_date > '2020-02-01'
    group by customer_id
    having count(customer_id)>=10

【问题讨论】:

  • 一方面,你甚至不说出了什么问题。没有反馈,没有错误信息等。
  • 这是订单,那么访问是什么?
  • 请提供表格描述一些数据示例和预期结果。如果启用only_full_group_by sql 模式,您的查询将产生错误

标签: mysql sql


【解决方案1】:

您不应在选择中混合分组列和“普通”列

select *
from table1
where customer_id in
(
    Select customer_id
    from Table1
    where order_date > '2020-02-01'
    group by customer_id
    having count(*) >= 10
)

【讨论】:

  • 谢谢!这非常有效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多