根据SQL中的另一个表显示所有日期的计数?

时间:2019-01-17 13:15:36

标签: mysql sql mysqli

我有两个表,一个是日历,第二个是final_registration,如下所示:

我的日历表:

emitter.onCompletion(() -> {
  f.cancel(true);
});

我的第二张桌子:

*--------------------*
| S.No. |  datefield |
*--------------------*
|  1    | 2019-01-01 |
|  2    | 2019-01-02 |
|  3    | 2019-01-03 |
|  4    | 2019-01-04 |
|  5    | 2019-01-05 |
|  6    | 2019-01-06 |
|  7    | 2019-01-07 |
|  8    | 2019-01-08 |
|  9    | 2019-01-09 |
|  10   | 2019-01-10 |
|  11   | 2019-01-11 |
|  12   | 2019-01-12 |
|  13   | 2019-01-13 |
|  14   | 2019-01-14 |
|  15   | 2019-01-15 |
|  16   | 2019-01-16 |
|  17   | 2019-01-17 |
|  18   | 2019-01-18 |
|  19   | 2019-01-19 |
|  20   | 2019-01-20 |
|  21   | 2019-01-21 |
|  22   | 2019-01-22 |
|  23   | 2019-01-23 |
|  24   | 2019-01-24 |
|  25   | 2019-01-25 |
|  26   | 2019-01-26 |
|  27   | 2019-01-27 |
|  28   | 2019-01-28 |
|  29   | 2019-01-29 |
|  30   | 2019-01-30 |
|  31   | 2019-01-31 |
---------------------

我正在尝试以下输出:

*-----------------------------------------*
| id | event_id |  name |    booking_date  |
*-----------------------------------------*
| 1  |   101    |  Ritu | 2019-01-15 13:21 |
| 2  |   101    | Seeta | 2019-01-15 18:21 |
| 3  |   101    | Geeta | 2019-01-16 13:21 |
| 4  |   102    | Wasim | 2019-01-16 14:21 |
| 5  |   102    | Rahul | 2019-01-17 13:21 |
| 6  |   101    | Gagan | 2019-01-17 14:21 |
| 7  |   101    | Sunny | 2019-01-17 15:21 |
| 8  |   101    | Aman  | 2019-01-17 16:21 |
-------------------------------------------

因此,与今天1月17日一样,我需要直到当前日期为止的输出。我已经尝试查询,但没有给我1月1日至1月14日的信息

*--------------------*
|  datefield | count |
*--------------------*
| 2019-01-01 |  0   |
| 2019-01-02 |  0   |
| 2019-01-03 |  0   |
| 2019-01-04 |  0   |
| 2019-01-05 |  0   |
| 2019-01-06 |  0   |
| 2019-01-07 |  0   |
| 2019-01-08 |  0   |
| 2019-01-09 |  0   |
| 2019-01-10 |  0   |
| 2019-01-11 |  0   |
| 2019-01-12 |  0   |
| 2019-01-13 |  0   |
| 2019-01-14 |  0   |
| 2019-01-15 |  2   |
| 2019-01-16 |  1   |
| 2019-01-17 |  3   |
---------------------

我的查询给了我以下结果:

SELECT calendar.datefield, COUNT(calendar.datefield)
FROM calendar
LEFT JOIN final_registration
    ON DATE_FORMAT(calendar.datefield, '%Y-%m-%d') = DATE_FORMAT(final_registration.booking_date, '%Y-%m-%d')
WHERE DATE_FORMAT(calendar.datefield, '%Y-%m-%d') <= DATE_FORMAT( CURDATE(), '%Y-%m-%d' )
  AND DATE_FORMAT(calendar.datefield, '%Y-%m') = DATE_FORMAT( CURDATE(), '%Y-%m' )
  AND final_registration.event_id = '101'
GROUP BY DATE_FORMAT(calendar.datefield, '%Y-%m-%d')

我尝试了很多,但无法获得结果。

2 个答案:

答案 0 :(得分:2)

通过在LEFT子句中包含条件,使INNER联接成为WHERE联接。试试这个:

SELECT calendar.datefield, COUNT(calendar.datefield)
FROM calendar
LEFT JOIN final_registration
    ON DATE_FORMAT(calendar.datefield, '%Y-%m-%d') = DATE_FORMAT
(final_registration.booking_date, '%Y-%m-%d')
  AND final_registration.event_id = '101'
WHERE DATE_FORMAT(calendar.datefield, '%Y-%m-%d') <= DATE_FORMAT( CURDATE(), '%Y-%m-%d' )
  AND DATE_FORMAT(calendar.datefield, '%Y-%m') = DATE_FORMAT( CURDATE(), '%Y-%m' )
GROUP BY DATE_FORMAT(calendar.datefield, '%Y-%m-%d')

答案 1 :(得分:0)

您需要将WHERE子句中的某些限制移至ON子句:

SELECT
    c.datefield,
    COUNT(f.booking_date) AS cnt
FROM calendar c
LEFT JOIN final_registration f
    ON c.datefield = DATE(f.booking_date) AND
       f.event_id = '101'
WHERE
    c.datefield BETWEEN DATE_FORMAT(NOW() ,'%Y-%m-01') AND CURDATE()
GROUP BY
    c.datefield;

enter image description here

Demo

请注意,您想对final_registration表中的字段进行计数,该表显示在左联接的右侧。

我的回答延迟是由于清理您的查询以删除对DATE_FORMAT的所有不必要的呼叫。您应该可以直接处理datefield。至于WHERE子句,似乎您只想要当月的日期,但不迟于当月的当前日期。