我有一张桌子:
table1
id value date
1 2 2014-10-11 17:24:16
2 3 2014-10-12 13:24:16
3 2 2014-10-12 15:24:16
4 2 2014-10-12 17:24:16
5 4 2014-10-14 11:24:16
6 2 2014-10-14 13:24:16
我想得到以下结果:
value date
2 2014-10-11
7 2014-10-12
0 2014-10-13
6 2014-10-14
我如何在mysql中执行此操作?感谢。
P> S:该行在哪里: 0 2014-10-13 ?
答案 0 :(得分:1)
尝试下面的内容:
SELECT SUM(value) AS value, DATE(date) As Date
FROM Table
GROUP BY DATE(date)
答案 1 :(得分:1)
SELECT SUM(value) AS value, DATE(date) AS date
FROM table1
GROUP BY DATE(date)
答案 2 :(得分:1)
试试这个
<?php
$a = mysqli_query($db, "SELECT SUM(value) AS value, DATE(date) AS date, DAYOFMONTH(date) AS day FROM mytable GROUP BY DATE(date)");
WHILE($a2 = mysqli_fetch_array($a))
{
if($daycheck == "")
{
$daycheck = $a2[day];
}
else
{
if(($daycheck + 1) != $a2[day])
{
for($x = ($daycheck+1); $x<$a2[day]; $x++)
{
$date_new2 = date('Y-m-d', strtotime($daycheck2 . ' + 1 day'));
echo "0-----".$date_new2."<br>";
$daycheck2 = $date_new2;
}
}
}
echo $a2[value]."-----".$a2[date]."<br>";
$daycheck = $a2[day];
$daycheck2 = $a2[date];
}
?>
这将显示结果中间没有日期的0值
答案 3 :(得分:1)
为了做你想做的事,你需要生成你范围内的所有日期,然后使用left join
。以下是一种方法:
select d.d, sum(t1.value) as value
from (select date('2014-10-11') as d union all
select date('2014-10-12') as d union all
select date('2014-10-13') as d union all
select date('2014-10-14') as d
) d left join
table1 t1
on date(t1.date) = d.d
group by d.d
order by d.d;
答案 4 :(得分:0)
SELECT sum(value) as value, DATE(date) FROM <table> GROUP BY DATE(date)
答案 5 :(得分:0)
尝试此查询:
Select sum(value), DATE(`date`) from table1 group by DATE(`date`)
答案 6 :(得分:0)
试试这个
SELECT SUM(value) AS value,
DATE(date) AS date
FROM table1
GROUP BY DATE(date)
答案 7 :(得分:0)
你必须使用分组
select sum(value) ,DATE_FORMAT(date,'%Y-%d-%m') as date from MyTable group by DATE_FORMAT(date,'%Y-%d-%m')