我需要修改我的查询以选择1天,1周的信息。 我有2个表用于投票,其他用于文章,它应该从文章表中查看st_date。
查询
SELECT stories.*, SUM(votes.vote_value) as 'total_votes'
FROM stories
JOIN votes ON stories.id = votes.item_name
GROUP BY stories.id
ORDER BY total_votes DESC LIMIT 10
数据库结构:
故事表
投票表
答案 0 :(得分:3)
一周总是7天,所以你实际上需要8天的数据。
WHERE st_date >= DATE_SUB(NOW(), INTERVAL 8 DAY)
您的完整查询将是:
SELECT stories.*, SUM(votes.vote_value) as 'total_votes'
FROM stories
JOIN votes ON stories.id = votes.item_name
WHERE stories.st_date >= DATE_SUB(NOW(), INTERVAL 8 DAY)
GROUP BY stories.id
ORDER BY total_votes DESC LIMIT 10
答案 1 :(得分:1)
看起来像......
WHERE st_date> =现在,间隔8天
答案 2 :(得分:1)
您需要添加WHERE语句
之类的东西SELECT stories.*, SUM(votes.vote_value) as 'total_votes'
FROM stories
JOIN votes ON stories.id = votes.item_name
WHERE PERIOD_DIFF(CURRENT_DATE , stories.st_date) < 1
GROUP BY stories.id
ORDER BY total_votes DESC LIMIT 10
答案 3 :(得分:0)
您可以使用DATEDIF()
mysql函数
答案 4 :(得分:0)
您可以在任何日期范围之间找到结果
st_date >= start_date AND st_date <=end_date