mysql - 查询Mysql数据库

标签 mysql database database-design

我有一个名为 Order 的表

-______________________
|Order_id | customer_id |
|_________|_____________|
|     1   |    a        |
|     2   |    b        |
|     3   |    c        |
|     4   |    a        |
|_________|_____________|

我需要一个查询来查找包含多个订单列表的 Customer_id,并将这些内容存储在一个新表中

最佳答案

Find the Customer_id which contains the list of more than one orders

所以只有 COUNT() 或任何给定 customer_id 的行?像这样:

SELECT COUNT(order_id) AS order_count, customer_id FROM test GROUP BY customer_id

这将返回:

order_count    customer_id
--------------------------
2              a
1              b
1              c

如果您想将结果进一步缩减为仅包含多个订单的结果,只需对其进行过滤即可。我的 SQL 有点生疏,但这应该可以解决问题:

SELECT * FROM
    (SELECT COUNT(order_id) AS order_count, customer_id FROM order GROUP BY customer_id) AS order_counts
WHERE order_count > 1

这会产生:

order_count    customer_id
--------------------------
2              a

关于mysql - 查询Mysql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35346709/

相关文章:

PHP安装配置: error: Cannot find php_pdo_driver. h

c# - 为什么 File.WriteAllBytes 不适用于 .db 文件但适用于 .csv 文件?

mysql - 数据库名称和用户名有什么区别?

mysql - 在关系数据库中构建搜索索引的策略

database - 为什么很少有完整的数据库模式在线样本?

mysql - 我应该将 EAV 值放入数据类型表中吗?

mysql 字符集,我可以在 python 中执行转换吗?

mysql - 无效参数编号 : number of bound variables does not match number of tokens

mysql - 如何按 block 将MYSQL数据导出到CSV?

php - 添加文章到 Jooma 时出现 500 内部服务器错误! 3.5 数据库