如何仅显示自从和直到日期数据

时间:2015-09-04 18:03:52

标签: php mysql sql

我有下一个sql代码

SELECT date,count(clics)
FROM advertising_clics
WHERE date > CURDATE() - INTERVAL 7 DAY
GROUP BY date

结果

date            count(clics)
2015-08-29          4
2015-08-30          1
2015-08-31          1
2015-09-01          1
2015-09-02          1

但我希望结果是这个

date            count(clics)
    2015-08-29          4
    2015-09-02          1

1 个答案:

答案 0 :(得分:0)

尝试获取第一条记录和最后一条记录,并将结果union作为

SELECT date,count(clics)
FROM advertising_clics
WHERE date > CURDATE() - INTERVAL 7 DAY
GROUP BY date
order by date
limit 1

union

SELECT date,count(clics)
FROM advertising_clics
WHERE date > CURDATE() - INTERVAL 7 DAY
GROUP BY date
order by date desc
limit 1