如何计算某些值在 SQL 中的 table 和 return 中出现的次数?

How to count how many times certain values appear in a table in SQL and return that number in a column?

我已经使用 COUNT 函数来确定 table 中有多少行或某个值在 table 中出现的频率。

但是,我想 return 将 table 中多个值的 'count' 作为单独的列。

假设我们有一位客户 table 有专栏;客户 ID #、姓名、Phone 号码。
假设我们还有一个销售额 table,其中包含以下列:客户 ID、购买的商品、日期

我想查询 return 客户 ID 列和客户 ID 在销售中出现次数的列 table。我想同时为我的所有客户 ID 执行此操作——有什么提示吗?

您想使用GROUP BY

Select Count(*), CustomerID
from Sales
GROUP BY CustomerID

您可以使用 group by:

select   customer_id,
         count(*)
from     sales  
group by customer_id

这将 return 按客户 ID 显示匹配项数量的行。