2005-06-23 01:51:10 +02:00
|
|
|
DROP TABLE IF EXISTS t1, t2;
|
2002-12-02 20:38:00 +01:00
|
|
|
CREATE TABLE t1 (a INT, b INT, c INT, UNIQUE (A), UNIQUE(B));
|
|
|
|
INSERT t1 VALUES (1,2,10), (3,4,20);
|
2002-12-02 22:04:26 +01:00
|
|
|
INSERT t1 VALUES (5,6,30) ON DUPLICATE KEY UPDATE c=c+100;
|
2002-12-02 20:38:00 +01:00
|
|
|
SELECT * FROM t1;
|
|
|
|
a b c
|
|
|
|
1 2 10
|
|
|
|
3 4 20
|
|
|
|
5 6 30
|
2002-12-02 22:04:26 +01:00
|
|
|
INSERT t1 VALUES (5,7,40) ON DUPLICATE KEY UPDATE c=c+100;
|
2002-12-02 20:38:00 +01:00
|
|
|
SELECT * FROM t1;
|
|
|
|
a b c
|
|
|
|
1 2 10
|
|
|
|
3 4 20
|
|
|
|
5 6 130
|
2002-12-02 22:04:26 +01:00
|
|
|
INSERT t1 VALUES (8,4,50) ON DUPLICATE KEY UPDATE c=c+1000;
|
2002-12-02 20:38:00 +01:00
|
|
|
SELECT * FROM t1;
|
|
|
|
a b c
|
|
|
|
1 2 10
|
|
|
|
3 4 1020
|
|
|
|
5 6 130
|
2002-12-02 22:04:26 +01:00
|
|
|
INSERT t1 VALUES (1,4,60) ON DUPLICATE KEY UPDATE c=c+10000;
|
2002-12-02 20:38:00 +01:00
|
|
|
SELECT * FROM t1;
|
|
|
|
a b c
|
|
|
|
1 2 10010
|
|
|
|
3 4 1020
|
|
|
|
5 6 130
|
2002-12-02 22:04:26 +01:00
|
|
|
INSERT t1 VALUES (1,9,70) ON DUPLICATE KEY UPDATE c=c+100000, b=4;
|
2006-01-23 12:17:05 +01:00
|
|
|
ERROR 23000: Duplicate entry '4' for key 'b'
|
2002-12-02 20:38:00 +01:00
|
|
|
SELECT * FROM t1;
|
|
|
|
a b c
|
|
|
|
1 2 10010
|
|
|
|
3 4 1020
|
|
|
|
5 6 130
|
|
|
|
TRUNCATE TABLE t1;
|
|
|
|
INSERT t1 VALUES (1,2,10), (3,4,20);
|
2002-12-02 22:04:26 +01:00
|
|
|
INSERT t1 VALUES (5,6,30), (7,4,40), (8,9,60) ON DUPLICATE KEY UPDATE c=c+100;
|
2002-12-02 20:38:00 +01:00
|
|
|
SELECT * FROM t1;
|
|
|
|
a b c
|
|
|
|
1 2 10
|
|
|
|
3 4 120
|
|
|
|
5 6 30
|
|
|
|
8 9 60
|
2002-12-02 22:04:26 +01:00
|
|
|
INSERT t1 SET a=5 ON DUPLICATE KEY UPDATE b=0;
|
2002-12-02 21:12:25 +01:00
|
|
|
SELECT * FROM t1;
|
|
|
|
a b c
|
|
|
|
1 2 10
|
|
|
|
3 4 120
|
|
|
|
5 0 30
|
|
|
|
8 9 60
|
2003-05-03 01:16:56 +02:00
|
|
|
INSERT t1 VALUES (2,1,11), (7,4,40) ON DUPLICATE KEY UPDATE c=c+VALUES(a);
|
|
|
|
SELECT *, VALUES(a) FROM t1;
|
|
|
|
a b c VALUES(a)
|
|
|
|
1 2 10 NULL
|
|
|
|
3 4 127 NULL
|
|
|
|
5 0 30 NULL
|
|
|
|
8 9 60 NULL
|
|
|
|
2 1 11 NULL
|
2003-10-30 11:57:26 +01:00
|
|
|
explain extended SELECT *, VALUES(a) FROM t1;
|
2006-07-28 19:27:01 +02:00
|
|
|
id select_type table type possible_keys key key_len ref rows filtered Extra
|
|
|
|
1 SIMPLE t1 ALL NULL NULL NULL NULL 5 100.00
|
2003-10-30 11:57:26 +01:00
|
|
|
Warnings:
|
2004-07-20 07:48:28 +02:00
|
|
|
Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b`,`test`.`t1`.`c` AS `c`,values(`test`.`t1`.`a`) AS `VALUES(a)` from `test`.`t1`
|
2003-10-30 11:57:26 +01:00
|
|
|
explain extended select * from t1 where values(a);
|
|
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
2006-09-08 09:24:14 +02:00
|
|
|
1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
|
2003-10-30 11:57:26 +01:00
|
|
|
Warnings:
|
2006-09-15 14:14:38 +02:00
|
|
|
Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b`,`test`.`t1`.`c` AS `c` from `test`.`t1` where values(`test`.`t1`.`a`)
|
2002-12-02 20:38:00 +01:00
|
|
|
DROP TABLE t1;
|
2004-03-15 18:36:16 +01:00
|
|
|
create table t1(a int primary key, b int);
|
|
|
|
insert into t1 values(1,1),(2,2),(3,3),(4,4),(5,5);
|
|
|
|
select * from t1;
|
|
|
|
a b
|
|
|
|
1 1
|
|
|
|
2 2
|
|
|
|
3 3
|
|
|
|
4 4
|
|
|
|
5 5
|
|
|
|
insert into t1 values(4,14),(5,15),(6,16),(7,17),(8,18)
|
|
|
|
on duplicate key update b=b+10;
|
|
|
|
affected rows: 7
|
|
|
|
info: Records: 5 Duplicates: 2 Warnings: 0
|
|
|
|
select * from t1;
|
|
|
|
a b
|
|
|
|
1 1
|
|
|
|
2 2
|
|
|
|
3 3
|
|
|
|
4 14
|
|
|
|
5 15
|
|
|
|
6 16
|
|
|
|
7 17
|
|
|
|
8 18
|
|
|
|
replace into t1 values(5,25),(6,26),(7,27),(8,28),(9,29);
|
|
|
|
affected rows: 9
|
|
|
|
info: Records: 5 Duplicates: 4 Warnings: 0
|
|
|
|
select * from t1;
|
|
|
|
a b
|
|
|
|
1 1
|
|
|
|
2 2
|
|
|
|
3 3
|
|
|
|
4 14
|
|
|
|
5 25
|
|
|
|
6 26
|
|
|
|
7 27
|
|
|
|
8 28
|
|
|
|
9 29
|
|
|
|
drop table t1;
|
2004-12-13 13:26:28 +01:00
|
|
|
CREATE TABLE t1 (a INT, b INT, c INT, UNIQUE (A), UNIQUE(B));
|
|
|
|
INSERT t1 VALUES (1,2,10), (3,4,20);
|
|
|
|
INSERT t1 SELECT 5,6,30 FROM DUAL ON DUPLICATE KEY UPDATE c=c+100;
|
|
|
|
SELECT * FROM t1;
|
|
|
|
a b c
|
|
|
|
1 2 10
|
|
|
|
3 4 20
|
|
|
|
5 6 30
|
|
|
|
INSERT t1 SELECT 5,7,40 FROM DUAL ON DUPLICATE KEY UPDATE c=c+100;
|
|
|
|
SELECT * FROM t1;
|
|
|
|
a b c
|
|
|
|
1 2 10
|
|
|
|
3 4 20
|
|
|
|
5 6 130
|
|
|
|
INSERT t1 SELECT 8,4,50 FROM DUAL ON DUPLICATE KEY UPDATE c=c+1000;
|
|
|
|
SELECT * FROM t1;
|
|
|
|
a b c
|
|
|
|
1 2 10
|
|
|
|
3 4 1020
|
|
|
|
5 6 130
|
|
|
|
INSERT t1 SELECT 1,4,60 FROM DUAL ON DUPLICATE KEY UPDATE c=c+10000;
|
|
|
|
SELECT * FROM t1;
|
|
|
|
a b c
|
|
|
|
1 2 10010
|
|
|
|
3 4 1020
|
|
|
|
5 6 130
|
|
|
|
INSERT t1 SELECT 1,9,70 FROM DUAL ON DUPLICATE KEY UPDATE c=c+100000, b=4;
|
2006-01-23 12:17:05 +01:00
|
|
|
ERROR 23000: Duplicate entry '4' for key 'b'
|
2004-12-13 13:26:28 +01:00
|
|
|
SELECT * FROM t1;
|
|
|
|
a b c
|
|
|
|
1 2 10010
|
|
|
|
3 4 1020
|
|
|
|
5 6 130
|
|
|
|
TRUNCATE TABLE t1;
|
|
|
|
INSERT t1 VALUES (1,2,10), (3,4,20);
|
2005-02-17 02:33:59 +01:00
|
|
|
CREATE TABLE t2 (a INT, b INT, c INT, d INT);
|
2004-12-13 13:26:28 +01:00
|
|
|
INSERT t2 VALUES (5,6,30,1), (7,4,40,1), (8,9,60,1);
|
|
|
|
INSERT t2 VALUES (2,1,11,2), (7,4,40,2);
|
2005-06-27 15:46:41 +02:00
|
|
|
INSERT t1 SELECT a,b,c FROM t2 WHERE d=1 ON DUPLICATE KEY UPDATE c=t1.c+100;
|
2004-12-13 13:26:28 +01:00
|
|
|
SELECT * FROM t1;
|
|
|
|
a b c
|
|
|
|
1 2 10
|
|
|
|
3 4 120
|
|
|
|
5 6 30
|
|
|
|
8 9 60
|
|
|
|
INSERT t1 SET a=5 ON DUPLICATE KEY UPDATE b=0;
|
|
|
|
SELECT * FROM t1;
|
|
|
|
a b c
|
|
|
|
1 2 10
|
|
|
|
3 4 120
|
|
|
|
5 0 30
|
|
|
|
8 9 60
|
2005-02-17 02:33:59 +01:00
|
|
|
INSERT t1 SELECT a,b,c FROM t2 WHERE d=2 ON DUPLICATE KEY UPDATE c=c+VALUES(a);
|
2005-06-27 15:46:41 +02:00
|
|
|
ERROR 23000: Column 'c' in field list is ambiguous
|
|
|
|
INSERT t1 SELECT a,b,c FROM t2 WHERE d=2 ON DUPLICATE KEY UPDATE c=t1.c+VALUES(t1.a);
|
2004-12-13 13:26:28 +01:00
|
|
|
SELECT *, VALUES(a) FROM t1;
|
|
|
|
a b c VALUES(a)
|
|
|
|
1 2 10 NULL
|
|
|
|
3 4 127 NULL
|
|
|
|
5 0 30 NULL
|
|
|
|
8 9 60 NULL
|
|
|
|
2 1 11 NULL
|
|
|
|
DROP TABLE t1;
|
|
|
|
DROP TABLE t2;
|
2005-05-14 15:24:36 +02:00
|
|
|
create table t1 (a int not null unique) engine=myisam;
|
2005-05-07 14:31:37 +02:00
|
|
|
insert into t1 values (1),(2);
|
|
|
|
insert ignore into t1 select 1 on duplicate key update a=2;
|
|
|
|
select * from t1;
|
|
|
|
a
|
|
|
|
1
|
|
|
|
2
|
2005-06-27 15:46:41 +02:00
|
|
|
insert ignore into t1 select a from t1 as t2 on duplicate key update a=t1.a+1 ;
|
2005-05-07 14:31:37 +02:00
|
|
|
select * from t1;
|
|
|
|
a
|
|
|
|
1
|
|
|
|
3
|
2005-05-14 15:24:36 +02:00
|
|
|
insert into t1 select 1 on duplicate key update a=2;
|
|
|
|
select * from t1;
|
|
|
|
a
|
|
|
|
2
|
|
|
|
3
|
|
|
|
insert into t1 select a from t1 on duplicate key update a=a+1 ;
|
2005-06-27 15:46:41 +02:00
|
|
|
ERROR 23000: Column 'a' in field list is ambiguous
|
|
|
|
insert ignore into t1 select a from t1 on duplicate key update a=t1.a+1 ;
|
|
|
|
ERROR 23000: Column 't1.a' in field list is ambiguous
|
2005-05-07 14:31:37 +02:00
|
|
|
drop table t1;
|
2005-08-07 04:16:15 +02:00
|
|
|
CREATE TABLE t1 (
|
|
|
|
a BIGINT(20) NOT NULL DEFAULT 0,
|
|
|
|
PRIMARY KEY (a)
|
|
|
|
) ENGINE=MyISAM;
|
|
|
|
INSERT INTO t1 ( a ) SELECT 0 ON DUPLICATE KEY UPDATE a = a + VALUES (a) ;
|
|
|
|
DROP TABLE t1;
|
2006-09-08 09:24:14 +02:00
|
|
|
CREATE TABLE t1
|
|
|
|
(
|
|
|
|
a BIGINT UNSIGNED,
|
|
|
|
b BIGINT UNSIGNED,
|
|
|
|
PRIMARY KEY (a)
|
|
|
|
);
|
|
|
|
INSERT INTO t1 VALUES (45, 1) ON DUPLICATE KEY UPDATE b =
|
|
|
|
IF(VALUES(b) > t1.b, VALUES(b), t1.b);
|
|
|
|
SELECT * FROM t1;
|
|
|
|
a b
|
|
|
|
45 1
|
|
|
|
INSERT INTO t1 VALUES (45, 2) ON DUPLICATE KEY UPDATE b =
|
|
|
|
IF(VALUES(b) > t1.b, VALUES(b), t1.b);
|
|
|
|
SELECT * FROM t1;
|
|
|
|
a b
|
|
|
|
45 2
|
|
|
|
INSERT INTO t1 VALUES (45, 1) ON DUPLICATE KEY UPDATE b =
|
|
|
|
IF(VALUES(b) > t1.b, VALUES(b), t1.b);
|
|
|
|
SELECT * FROM t1;
|
|
|
|
a b
|
|
|
|
45 2
|
|
|
|
DROP TABLE t1;
|