使用null结果选择每月的所有数据

时间:2016-06-30 11:13:26

标签: mysql

我能做到吗?

------------------------
date        |total|
2016-01-12  | 1   |
2016-01-13  | 2   |
2016-01-14  | 0   |
2016-01-15  | 1   |

使用mysql查询?我试过但我的结果是

date        |total|
2016-01-12  | 1   |
2016-01-13  | 2   |
2016-01-15  | 1   |

myquery只显示有结果的日期

这是我的疑问:

select day(tgl_daftar) as tanggal, count(day(tgl_daftar)) as jumlah 
from pasien 
where month(tgl_daftar) = '06' and year(tgl_daftar) = '2016' 
group by day(tgl_daftar)

这是我的表:

Field                 Type         Collation          Null    Key     Default  Extra   Privileges                       Comment
--------------------  -----------  -----------------  ------  ------  -------  ------  -------------------------------  -------
NO_PASIEN             char(6)      latin1_swedish_ci  NO      PRI     (NULL)           select,insert,update,references         
ID_KOTA               int(11)      (NULL)             NO      MUL     (NULL)           select,insert,update,references         
ID_AGAMA              int(11)      (NULL)             NO      MUL     (NULL)           select,insert,update,references         
ID_PEKERJAAN          int(11)      (NULL)             NO      MUL     (NULL)           select,insert,update,references         
NAMA_PASIEN           varchar(30)  latin1_swedish_ci  YES             (NULL)           select,insert,update,references         
JENIS_KELAMIN_PASIEN  char(2)      latin1_swedish_ci  YES             (NULL)           select,insert,update,references         
TGLLAHIR_PASIEN       date         (NULL)             YES             (NULL)           select,insert,update,references         
NO_TELPON_PAS         int(11)      (NULL)             YES             (NULL)           select,insert,update,references         
ALAMAT_PASIEN         varchar(50)  latin1_swedish_ci  YES             (NULL)           select,insert,update,references         
TGL_DAFTAR            date         (NULL)             YES             (NULL)           select,insert,update,references         

任何想法。?

1 个答案:

答案 0 :(得分:0)

在您的查询中尝试此操作

    SELECT 
            `date`, SUM(<somefield>) AS `total`
    FROM 
            <your_table>
    GROUP BY
            `date`
    HAVING 
            `total`    >    0;

您的查询,

    SELECT 
            DAY(tgl_daftar) AS tanggal, 
            COUNT(day(tgl_daftar)) AS jumlah 
    FROM 
            pasien 
    WHERE 
            MONTH(tgl_daftar) = '06' AND YEAR(tgl_daftar) = '2016' 
    GROUP BY 
            DAY(tgl_daftar)
    HAVING 
                `jumlah`    >    0;