我有这张桌子:
CREATE TABLE `count_traffic` (
`tool_id` int(11) NOT NULL,
`leads` int(11) NOT NULL DEFAULT '0',
`date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
关注SELECT:
$query="
SELECT sum(`leads`) as leads
, date
FROM count_traffic
WHERE tool_id = :tool_id
AND date BETWEEN '2018-03-13' AND '2018-03-14'
GROUP
BY date";
如果我使用date
作为日期字段,这个SELECT会显示出良好的效果!我如何才能从datetime
字段类型中选择两个日期和GROUP BY DATE?
答案 0 :(得分:0)
您使用DATE()函数并将date
列包装在该函数调用中。
SELECT sum(`leads`) as leads
, DATE(`date`)
FROM count_traffic
WHERE tool_id = :tool_id
AND date BETWEEN '2018-03-13' AND '2018-03-14'
GROUP
BY DATE(`date`)
答案 1 :(得分:0)
我找到了解决方案,这也是需要的人选择的选择:
SELECT sum(`leads`) as leads, DATE_FORMAT(date,'%y-%m-%d') as date
FROM count_traffic
WHERE
tool_id = :tool_id
AND
DATE_FORMAT(date,'%Y-%m-%d') BETWEEN '2018-03-13' AND '2018-03-14'
GROUP BY DATE_FORMAT(date,'%Y-%m-%d');
答案 2 :(得分:0)
下面我附上了小提琴链接......你检查一下...... http://sqlfiddle.com/#!9/6b1ebb/6