mysql - 统计生成超过 2 张发票的客户 - mysql

标签 mysql sql

我想统计生成超过 2 张发票的客户数量

mysql> select * from invoices;
+-------+--------+---------------------+
| invId | cus_id | timeinfo            |
+-------+--------+---------------------+
|     1 |     36 | 2019-02-21 16:47:10 |
|     2 |     33 | 2019-02-21 16:49:22 |
|     3 |     34 | 2019-02-21 16:51:00 |
|     7 |     34 | 2019-02-21 19:02:46 |
|     5 |     36 | 2019-02-21 16:53:55 |
|     6 |     33 | 2019-02-21 19:02:02 |
|     8 |     33 | 2019-02-22 01:36:26 |
|     9 |     34 | 2019-02-22 16:01:20 |
|    10 |     33 | 2019-03-03 13:02:28 |
|    11 |     35 | 2019-03-05 19:11:53 |
|    12 |     32 | 2019-03-13 22:41:38 |
+-------+--------+---------------------+
11 rows in set (0.00 sec)

最佳答案

尝试如下

这将返回拥有多张发票的客户 ID

 select cus_id 
 from invoices
 group by cus_id 
 having count(*)>2

如果您只需要号码,则使用下面的内容将仅返回客户号码

select count(*) as numberofcustomer
 from (   select cus_id 
     from invoices
     group by cus_id 
    having count(*)>2 
     ) a

关于mysql - 统计生成超过 2 张发票的客户 - mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55148937/

相关文章:

mysql - 将 CSV 数据导入 SQL 数据库

mysql - 将 C 代码程序包装到 R : alternatives to Rcpp?

php - MySQL REGEXP 与?占位符如何与 php 变量绑定(bind)

Php和Sql,非undefined时为undefined index

mysql - 在同一台服务器上导入两个具有相同架构(数据库名称)的不同 mysql 转储文件

php - MySQL/PHP foreach 仍然只在数据库中显示第一个

php - 使用准备好的语句将多个值插入到多行中 PHP、MYSQL

MySQL 计算一列的唯一性,而其他列在 Group By 中具有单个值

sql - 在 Oracle SQL 中声明要在查询中使用的变量

sql - 从数字中删除数字,Sql server