Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

MySQL:selecione todas as datas entre o intervalo de datas e obtenha datas correspondentes aos dados da tabela


Eu descobri isso modificando a consulta fornecida na solução para obter todas as datas.

A consulta a seguir retorna todas as datas e conta os IDs se houver algum registro:
select d.date, count(v.id) from 
(select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) date from
 (select 0 i 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) t0,
 (select 0 i 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) t1,
 (select 0 i 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) t2,
 (select 0 i 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) t3,
 (select 0 i 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) t4) d
left join visitors v on d.date = v.date
where d.date between '2016-06-01' and '2016-06-30'
group by d.date
order by d.date

A cortesia para obter o intervalo de datas vai para @mark-bannister e uma junção simples na consulta correspondente aos resultados, e a classificação obtém a solução.