1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
drop table if exists t1;
CREATE TABLE t1 (
visitor_id int(10) unsigned DEFAULT '0' NOT NULL,
group_id int(10) unsigned DEFAULT '0' NOT NULL,
hits int(10) unsigned DEFAULT '0' NOT NULL,
sessions int(10) unsigned DEFAULT '0' NOT NULL,
ts timestamp,
PRIMARY KEY (visitor_id,group_id)
)/*! engine=MyISAM */;
INSERT INTO t1 VALUES (465931136,7,2,2,20000318160952);
INSERT INTO t1 VALUES (173865424,2,2,2,20000318233615);
INSERT INTO t1 VALUES (173865424,8,2,2,20000318233615);
INSERT INTO t1 VALUES (173865424,39,2,2,20000318233615);
INSERT INTO t1 VALUES (173865424,7,2,2,20000318233615);
INSERT INTO t1 VALUES (173865424,3,2,2,20000318233615);
INSERT INTO t1 VALUES (173865424,6,2,2,20000318233615);
INSERT INTO t1 VALUES (173865424,60,2,2,20000318233615);
INSERT INTO t1 VALUES (173865424,1502,2,2,20000318233615);
INSERT INTO t1 VALUES (48985536,2,2,2,20000319013932);
INSERT INTO t1 VALUES (48985536,8,2,2,20000319013932);
INSERT INTO t1 VALUES (48985536,39,2,2,20000319013932);
INSERT INTO t1 VALUES (48985536,7,2,2,20000319013932);
INSERT INTO t1 VALUES (465931136,3,2,2,20000318160951);
INSERT INTO t1 VALUES (465931136,119,1,1,20000318160953);
INSERT INTO t1 VALUES (465931136,2,1,1,20000318160950);
INSERT INTO t1 VALUES (465931136,8,1,1,20000318160950);
INSERT INTO t1 VALUES (465931136,39,1,1,20000318160950);
INSERT INTO t1 VALUES (1092858576,14,1,1,20000319013445);
INSERT INTO t1 VALUES (357917728,3,2,2,20000319145026);
INSERT INTO t1 VALUES (357917728,7,2,2,20000319145027);
select visitor_id,max(ts) as mts from t1 group by visitor_id
having mts < DATE_SUB(NOW(),INTERVAL 3 MONTH);
visitor_id mts
48985536 2000-03-19 01:39:32
173865424 2000-03-18 23:36:15
357917728 2000-03-19 14:50:27
465931136 2000-03-18 16:09:53
1092858576 2000-03-19 01:34:45
select visitor_id,max(ts) as mts from t1 group by visitor_id
having DATE_ADD(mts,INTERVAL 3 MONTH) < NOW();
visitor_id mts
48985536 2000-03-19 01:39:32
173865424 2000-03-18 23:36:15
357917728 2000-03-19 14:50:27
465931136 2000-03-18 16:09:53
1092858576 2000-03-19 01:34:45
drop table t1;
set sql_mode='traditional';
create table t1 (d date);
insert into t1 (d) select date_sub('2000-01-01', INTERVAL 2001 YEAR);
ERROR 22008: Datetime function: datetime field overflow
insert into t1 (d) select date_add('2000-01-01',interval 8000 year);
ERROR 22008: Datetime function: datetime field overflow
insert into t1 values (date_add(NULL, INTERVAL 1 DAY));
insert into t1 values (date_add('2000-01-04', INTERVAL NULL DAY));
set sql_mode='';
insert into t1 (d) select date_sub('2000-01-01', INTERVAL 2001 YEAR);
Warnings:
Warning 1441 Datetime function: datetime field overflow
insert into t1 (d) select date_add('2000-01-01',interval 8000 year);
Warnings:
Warning 1441 Datetime function: datetime field overflow
insert into t1 values (date_add(NULL, INTERVAL 1 DAY));
insert into t1 values (date_add('2000-01-04', INTERVAL NULL DAY));
select * from t1;
d
NULL
NULL
NULL
NULL
NULL
NULL
drop table t1;
End of 4.1 tests
SELECT CAST('2006-09-26' AS DATE) + INTERVAL 1 DAY;
CAST('2006-09-26' AS DATE) + INTERVAL 1 DAY
2006-09-27
SELECT CAST('2006-09-26' AS DATE) + INTERVAL 1 MONTH;
CAST('2006-09-26' AS DATE) + INTERVAL 1 MONTH
2006-10-26
SELECT CAST('2006-09-26' AS DATE) + INTERVAL 1 YEAR;
CAST('2006-09-26' AS DATE) + INTERVAL 1 YEAR
2007-09-26
SELECT CAST('2006-09-26' AS DATE) + INTERVAL 1 WEEK;
CAST('2006-09-26' AS DATE) + INTERVAL 1 WEEK
2006-10-03
create table t1 (a int, b varchar(10));
insert into t1 values (1, '2001-01-01'),(2, '2002-02-02');
select '2007-01-01' + interval a day from t1;
'2007-01-01' + interval a day
2007-01-02
2007-01-03
select b + interval a day from t1;
b + interval a day
2001-01-02
2002-02-04
drop table t1;
End of 5.0 tests
|