在 MySQL 中使用 INTERVAL 和 CURDATE
我正在构建一个图表,我想接收每个月的数据。
这是我的第一个正在运行的请求:
SELECT s.GSP_nom AS nom, timestamp, AVG( v.vote + v.prix ) /2 AS avg
FROM votes_serveur AS v
INNER JOIN serveur AS s ON v.idServ = s.idServ
WHERE s.valide =1
AND v.date > CURDATE() -30
GROUP BY s.GSP_nom
ORDER BY avg DESC
但是,就我而言,我必须编写 12 个请求来接收前 12 个月的数据,是否有任何技巧可以避免写入:
// example for the previous month
AND v.date > CURDATE() -60
AND v.date < CURDATE () -30
我听说过 INTERVAL,我去了 MySQL 文档,但我没能实现它。
请问有使用 INTERVAL 的例子吗?
I'm building a chart and I want to receive data for each month.
Here's my first request which is working:
SELECT s.GSP_nom AS nom, timestamp, AVG( v.vote + v.prix ) /2 AS avg
FROM votes_serveur AS v
INNER JOIN serveur AS s ON v.idServ = s.idServ
WHERE s.valide =1
AND v.date > CURDATE() -30
GROUP BY s.GSP_nom
ORDER BY avg DESC
But, in my case I've to write 12 request to receive data for the 12 previous months, is there any trick to avoid writing:
// example for the previous month
AND v.date > CURDATE() -60
AND v.date < CURDATE () -30
I heard about INTERVAL, I went to the MySQL doc but i didn't manage to implement it.
Any example of using INTERVAL please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要
DATE_ADD/ DATE_SUB
:应该可以。
You need
DATE_ADD/DATE_SUB
:should work.
正如 A Star 所建议的,我总是使用类似这样的内容:
同样你可以这样做:
等等。比一直输入
DATE_ADD
或DATE_SUB
容易得多:)!As suggested by A Star, I always use something along the lines of:
Similarly you can do:
and so on. Much easier than typing
DATE_ADD
orDATE_SUB
all the time :)!我通常使用
Which 几乎与 Pekka 相同,但是这样你可以控制你的 INTERVAL 为负数或正数......
I usually use
Which is almost same as Pekka's but this way you can control your INTERVAL to be negative or positive...