根据过去的时间更改MySql值

时间:2015-05-06 13:33:44

标签: mysql sql

我有下表:

user_id     post_streak   streak_date          streak       first_name  club_id
--------    -----------   ------------         ---------    ----------- --------
18941684    1             2015-05-05 15:36:18    3           user       1000

如果长度超过12天,我想将条纹改为0。

当前查询:

select 
     first_name, streak, user_id from myTable 
where 
     club_id = 1000 
and 
     post_streak = 1 
and 
     streak_date between date_sub(now(),INTERVAL 12 DAY) and now() 
order by streak desc;

未显示超过12天的结果。我希望显示所有结果,但如果它已经延长了12天,则将“条纹”更改为0.

最好的方法是什么?

3 个答案:

答案 0 :(得分:0)

将条件放在select,而不是where

select first_name,
       (case when streak_date between date_sub(now(), INTERVAL 12 DAY) and now()  
             then streak
             else 0
        end) as streak,
       user_id from myTable 
where club_id = 1000 
order by streak desc;

我不确定post_streak子句中是否需要where条件。

答案 1 :(得分:0)

#import "HistoryCustomCell.h"

@implementation HistoryCustomCell {
    NSTrackingArea *_trackingArea;
    NSEvent *event;
}

- (void)awakeFromNib {
    [self createTrackingArea];
}

- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];

    // Drawing code here.
}

- (void)createTrackingArea {
    _trackingArea = [[NSTrackingArea alloc] initWithRect:self.bounds options:NSTrackingMouseEnteredAndExited|NSTrackingActiveInActiveApp owner:self userInfo:nil];
    [self addTrackingArea:_trackingArea];

    NSPoint mouseLocation = [[self window] mouseLocationOutsideOfEventStream];
    mouseLocation = [self convertPoint:mouseLocation fromView:nil];

    if (NSPointInRect(mouseLocation, [self bounds])) {
        [self mouseEntered:nil];
    } else {
        [self mouseExited:nil];
    }
}

- (void)mouseEntered:(NSEvent *)theEvent {
    [self.onMouseOverImage setImage:[NSImage imageNamed:@"mouse_over_back_cell.png"]];
}

- (void)mouseExited:(NSEvent *)theEvent {
    [self.onMouseOverImage setImage:[NSImage imageNamed:nil]];
}

@end

对于streak_date超过12天前的记录,第一个查询会将所有条纹值设置为0 第二个查询将获得所有记录的列表,其中club_id为1000,po​​st_streak为1

答案 2 :(得分:0)

http://sqlfiddle.com/#!9/d8bbd/6

select 
     user_id,
     first_name, 
     streak_date,
     IF(streak_date between date_sub(now(),INTERVAL 12 DAY) and now(),streak,0) 
from myTable 
where 
     club_id = 1000 
and 
     post_streak = 1 
order by streak desc;