doutang1856 2010-06-04 13:30
浏览 887
已采纳

MySQL:获取结果存在于一个时间范围内但不存在于另一个时间范围内

I have generated a dataset that contains data spanning thirty days. Im trying to issolate new data elements that have appeared in the last 2 days but not in the previous 28 days before that.

  1. I run a PHP script that generates the test data. (PHP and MYSQL return the same time when tested)

  2. I run the following query against it.

  3. Results are returned accuretly for aproximetly half an hour. Then despite the fact I believe there to be matching records none are returned when running this query.

Is there any obvious mistake I'm making in the SQL that would cause this apparent 'drift' to occur?

About The Data:

The script generates a 'race' per day. It populates the ranking tables with ranking of the 10 'jokeys'. For the purposes of testing the script generates races from the previous 2 days with 2 new 'jokeys' in the top 10. The remaining 30 days the races are identical.

Results Expected:

The names of two jokeys who have recently ranked in a race (in the last two days and have not ranked in the previous 28).

The SQL:

SELECT *, FROM_UNIXTIME(`race_timestamp`) as ts FROM `rankings`
WHERE `race_venue` = UNHEX(MD5('someplace'))
AND `jokey` IN
  (
SELECT `jokey`
FROM `rankings`
 WHERE `race_timestamp`
BETWEEN # Get results for races between now and two days ago
UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 2 DAY)) # timestamp two days ago
 AND
UNIX_TIMESTAMP() # time stamp now
   )
AND 
`jokey` NOT IN
(SELECT `jokey`
 FROM `rankings`
 WHERE `race_timestamp`
 BETWEEN  # Get results between 2 and 30 days ago
 UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 30 DAY)) # time stamp 30 days ago
 AND
 UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 2 DAY)) # time stamp 2 days ago
 )
GROUP BY jockey;

Hope someone can help! Ben

  • 写回答

2条回答 默认 最新

  • doumu2172 2010-06-04 13:56
    关注

    If you want to do this by date, rather than by the exact minute and second, you might change:

    UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 30 DAY))
    

    to something like:

    DATE(DATE_SUB(NOW(), INTERVAL 30 DAY))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题