【问题标题】:Count after joining two tables加入两个表后计数
【发布时间】:2018-12-18 13:32:59
【问题描述】:

我有以下表格:

约会

id |    date    |   time
1  | 2018-12-02 | 10:00:00
2  | 2018-12-05 | 12:00:00
3  | 2018-12-12 | 16:00:00
4  | 2018-12-12 | 17:00:00
5  | 2018-12-13 | 09:00:00

appointment_services

id | appointment_id | service_id
1  |        1       |     24
2  |        2       |     24
3  |        3       |     21
4  |        4       |     24
5  |        5       |     18

我想从约会表中搜索一个日期段,并从约会服务表中计算每个 service_id。

所以最终的结果是

service_id | times
     24    |   3
     21    |   1
     18    |   1

这是我到目前为止所做的

SELECT * FROM `appointment` a
INNER JOIN appointment_services s ON a.id = s.appointment_id
WHERE a.date BETWEEN '2018-12-10' AND '2018-12-18'

【问题讨论】:

  • 将日期和时间存储为单个实体

标签: mysql join count


【解决方案1】:

你很亲密:

select s.service_id, count(*) as times
from appointment_services s
join appintment a on a.id = s.appointment_id
where a.date between '2018-12-10' and '2018-12-18'
group by s.service_id

【讨论】:

    【解决方案2】:

    您可以在下面尝试 - 使用 count() 聚合和分组

     SELECT s.service_id, count(*) as cnttimes
        FROM `appointment` a
        INNER JOIN appointment_services s ON a.id = s.appointment_id
        WHERE a.date BETWEEN '2018-12-10' AND '2018-12-18'
        group by s.service_id
    

    【讨论】:

      【解决方案3】:

      您必须使用帮助GROUP BY 语句按 service_id 列对数据进行分组。例如:

      SELECT s.service_id, 
             count(*) as times
      FROM `appointment` a
      INNER JOIN appointment_services s ON a.id = s.appointment_id
      WHERE a.date BETWEEN '2018-12-10' AND '2018-12-18'
      GROUP BY s.service_id
      

      【讨论】:

        【解决方案4】:

        我建议将appointment_services 表加入到appointment 上的子查询中,并限制您想要的日期范围。这将允许我们保留所有服务价值,即使没有匹配的约会。

        SELECT
            s.service_id,
            COUNT(a.id) AS times
        FROM appointment_services s
        LEFT JOIN
        (
            SELECT id
            FROM appointment
            WHERE date BETWEEN '2018-12-10' AND '2018-12-18'
        ) a
            ON s.appointment_id = a.id
        GROUP BY
            s.service_id;
        

        Demo

        请注意,我有意更改了 service_id = 18 的数据,以使其单次约会在您想要的日期范围内。使用我建议的方法,我们仍然以零计数报告18。进行直接内连接会完全过滤掉18,它会出现在结果集中。

        【讨论】:

          【解决方案5】:

          http://sqlfiddle.com/#!9/1ad0f03/1

          SELECT s.service_id, COUNT(*) 
          FROM `appointment_services` s
          LEFT JOIN  appointment a 
          ON a.id = s.appointment_id
          WHERE a.date BETWEEN '2018-12-10' AND '2018-12-18'
          GROUP BY s.service_id
          

          您的预期结果的问题是您的 WHERE 子句减少了一些记录,因此有效结果是:

          service_id COUNT(*)
          18          1
          21          1
          24          1
          

          【讨论】:

            猜你喜欢
            • 2014-02-23
            • 1970-01-01
            • 2012-04-11
            • 1970-01-01
            • 1970-01-01
            • 2018-05-20
            • 1970-01-01
            • 2015-11-22
            • 2016-01-27
            相关资源
            最近更新 更多