选择两个日期之间的所有数据
有必要为某些日期选择值,我知道如何做到这一点,但有一个问题无法解决。
我在 MySQL 中的表:
[User]. [Wins]. [DATE]
#
Ivan ....... 4 ..... 05/06/2010
#
Ivan ....... 3 ..... 06/15/2010
#
Ivan ........ 6 ..... 06/18/2010
#
Ivan ........ 1 ..... 29/06/2010
问题是,如果用户没有访问过该站点,则不会在数据库中创建一行,并且会错过获取的日期。
如何获得统计数据(例如胜利 0),例如从 06/06/2010 到 06/14/2010 期间?帮忙做一下这个查询。
It is necessary to choose values for some dates, how to do this I know, but there is one problem that does not work to solve.
My table in MySQL:
[User]. [Wins]. [DATE]
#
Ivan ....... 4 ..... 05/06/2010
#
Ivan ....... 3 ..... 06/15/2010
#
Ivan ........ 6 ..... 06/18/2010
#
Ivan ........ 1 ..... 29/06/2010
The problem is that if the user has not visited the site, a row in the database is not created and missed the date obtained.
How do I get a statistics (such Wins 0) such as the period from 06/06/2010 to 06/14/2010? Help make a make this query.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
MySQL 没有递归功能,因此您只能使用 NUMBERS 表技巧 -
创建一个仅保存递增数字的表 - 使用 auto_increment 很容易做到:
使用以下方式填充表:
...您需要的任意数量的值。
使用DATE_ADD< /a> 构建日期列表,根据 NUMBERS.id 值增加天数:
<前><代码>选择 x.*
FROM (SELECT DATE_FORMAT(DATE_ADD('2010-06-06', INTERVAL n.id - 1 DAY), '%m/%d/%Y')
从数字 n
WHERE DATE_ADD('2010-06-06', INTERVAL n.id -1 DAY) <= '2010-06-14 ) x
根据时间部分 LEFT JOIN 到数据表:
MySQL doesn't have recursive functionality, so you're left with using the NUMBERS table trick -
Create a table that only holds incrementing numbers - easy to do using an auto_increment:
Populate the table using:
...for as many values as you need.
Use DATE_ADD to construct a list of dates, increasing the days based on the NUMBERS.id value:
LEFT JOIN onto your table of data based on the time portion:
尝试在 'wins' 字段上使用 'sum' 聚合函数,并将其(使用 where)限制为特定日期范围(因此 date<='6/14/2010' AND date>='06/06/2010')。
所以像
SELECT SUM(wins) FROM [tbl_name] WHERE [DATE] Between [date1] AND [date2]
以获得每个日期范围内的获胜次数;添加“按 [用户] 分组”,以便查看每个用户在该日期范围内的获胜次数。
try the 'sum' aggregate function on the 'wins' field and restrict it (using where) to that specific date range (so date<='6/14/2010' AND date>='06/06/2010').
So something like
SELECT SUM(wins) FROM [tbl_name] WHERE [DATE] between [date1] AND [date2]
to get the number of wins in each date range; add a 'group by [user]' in order to see the number of wins each user had in that date range.
这是我怀疑可以解决问题的答案。我创建了一个临时表来为您提供日期列表(我使用带有开始日期和结束日期的循环插入该列表。计数器必须从 0 开始,因为您想包含开始日期本身)。
然后,我将 DateList 连接到 Wins 表,为您提供该范围内所有日期、用户及其在这些日期的访问的列表。我还没有测试过它,但我怀疑代码接近于您所需要的。
编辑:我对我的连接和计数做了一些更改。我已经测试了MSSQL版本并且工作正常。
Here's an answer i suspect would do the trick. I create a temporary table to give you a list of the dates (which i insert using a loop with the start and end date. The counter has to start at 0 because you want to include the start date itself).
Then, i just join the DateList to the Wins table to give you the list of all dates in the range, the users and their visits on those dates. I haven't tested it but i suspect the code is close to being exactly what you need.
Edit: I made some changes to my joins and my count. I've tested the MSSQL version and it works fine.