如果count(*)为null并且该计数的关联日期为null,则返回0

时间:2016-05-11 07:31:18

标签: mysql

我有以下查询:

    SELECT DATE(leads_update_on), IFNULL(COUNT(*),0) leads 
    FROM tbl_leads 
    WHERE project_id=4 
    AND DATE(leads_update_on) >= DATE_SUB('2016-05-11', INTERVAL 6 DAY) 
    GROUP BY DATE(leads_update_on)

`

目前我只有2016-05-062016-05-07的记录,以上查询会返回结果,如

           `DATE(leads_update_on)|leads
           ----------------------|-----
           2016-05-06            |    7
           2016-05-07            |    4`

但我希望结果像

           `DATE(leads_update_on)|leads
           ----------------------|-----
           2016-05-05            |    0
           2016-05-06            |    7
           2016-05-07            |    4
           2016-05-08            |    0
           2016-05-09            |    0
           2016-05-10            |    0
           2016-05-11            |    0`

此处DATE_SUB('**2016-05-11**', INTERVAL **6** DAY)是动态的,取决于用户输入。

1 个答案:

答案 0 :(得分:1)

在数据库中,要获取日期,您必须连续排列日期;既然你没有全部拥有它们,你必须先生成它们。这将生成输入日期前1000天的列表,并将其与您的查询相关联:

TextView

您必须将两个输入值public void RecordButton (View view) { if(mRecorder == null){ audioFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/myaudio.3gp"; mRecorder = new MediaRecorder(); mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mRecorder.setOutputFile(audioFilePath); mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); } if (isRecording) { try{ stopRecording(); isRecording = false; ((Button)view).setText("start"); }catch(Exception e){ e.printStackTrace(); } } else { try{ startRecording(); isRecording = true; ((Button)view).setText("stop"); }catch(Exception e){ e.printStackTrace(); } } } public void startRecording() throws IllegalStateException, IOException{ mRecorder.prepare(); mRecorder.start(); } public void stopRecording() throws IllegalStateException, IOException{ mRecorder.stop(); mRecorder.release(); } select DateBase.BaseDate, ifnull(l.leads, 0) as leads from ( SELECT DATE(leads_update_on) as leads_update_on, COUNT(*) as leads FROM tbl_leads WHERE project_id=4 AND DATE(leads_update_on) >= DATE_SUB('2016-05-11', INTERVAL 6 DAY) GROUP BY DATE(leads_update_on) ) l right outer join ( SELECT DATE_SUB('2016-05-11', INTERVAL INTPART1.n + INTPART2.n * 10 + INTPART3.n * 100 day) as BaseDate FROM (SELECT 0 as n UNION SELECT 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) INTPART1 cross join (SELECT 0 as n UNION SELECT 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) INTPART2 cross join (SELECT 0 as n UNION SELECT 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) INTPART3 where INTPART1.n + INTPART2.n * 10 + INTPART3.n * 100 <= 6 ) as DateBase on l.leads_update_on = DateBase.BaseDate; 放入。

它只会&#34;只有&#34;生成1000天,如果有可能需要更多,你必须添加另一个连接(如果你只需要100,你可以摆脱一个连接)。如果您经常需要该查询,则可以生成一个只包含整数的整数基表。 0到1000所以你不必每次都生成它。

结果将只包括您的示例中的日期6。如果您的表中有2016-05-11的条目,则您的查询将包含它,因为您没有上限。但是你的示例输出有一个上限,具体取决于输入日期,所以我假设你打算这样做,我的查询将不包括2016-05-11。否则,您必须将2016-05-15中的日期设置为数据库中的最高日期。 (例如,按2016-05-15替换日期)或指定您需要的内容。