Mysql 根据与给定日期相等的最大值(table2_日期)获取计数(table1_id)。但是每个table1\u id都有许多table2\u日期

Mysql 根据与给定日期相等的最大值(table2_日期)获取计数(table1_id)。但是每个table1\u id都有许多table2\u日期,mysql,Mysql,我有两个表,其中包含以下数据 表1:客户 |---|-----| |id |name | |---|-----| |1 |Bob | |---|-----| |2 |Alice| |---|-----| 表2:订阅 |---|------------|-------------| |id |customer_id |end_date | |---|------------|-------------| |1 |1 |2020-03-16 | |---|-

我有两个表,其中包含以下数据

表1:客户

|---|-----|
|id |name |
|---|-----|
|1  |Bob  |
|---|-----|
|2  |Alice|
|---|-----|
表2:订阅

|---|------------|-------------|
|id |customer_id |end_date     |
|---|------------|-------------|
|1  |1           |2020-03-16   |
|---|------------|-------------|
|2  |1           |2020-04-05   |
|---|------------|-------------|
|3  |2           |2020-02-05   |
|---|------------|-------------|
|4  |1           |2020-05-04   |
|---|------------|-------------|
|5  |2           |2020-03-16   |
|---|------------|-------------|
我需要每个客户的max(subscriptions.end_date)等于今天的客户数

我当前的查询

选择计数(customers.id)、最大值(customer\u payments.package\u end\u date)
来自“客户”
在“customer\u payments”上内部加入“customer\u payments”。“customer\u id”=“customers”。“id”
按“客户付款”分组。“套餐结束日期”
日期(客户付款。套餐结束日期)=“2020-03-16”
它还将截止日期与今天相同的客户退回;但不是最伟大的日期。在给定的数据中,如果我运行今天日期的查询,则得到计数2。它同时选择customer_id 1和customer_id 2,即使customer_id 1的日期比今天最长

我还尝试了中提到的查询,也得到了相同的输出

试试这个

select count(id) as counted, id from subscriptions where customer_id in (select customer_id as avail_customers from subscriptions where end_date = '2020-03-16' group by customer_id) group by customer_id

粘贴您的sql…很抱歉,在运行sql时编辑了问题。