插入多行时,如何使用“ N DUPLICATE KEY UPDATE”增加MySql中的字段?
技术问答
282 人阅读
|
0 人回复
|
2023-09-14
|
插入多行时,如何使用“ ON DUPLICATE KEY UPDATE”增加MySql中的字段?
0 q) @% w' |; y+ I对于一行:
]+ j& D( N: H' y0 O- m/ a1 z- }INSERT INTO table
) B* Y9 f7 h5 u H) T (a, counter_elem) 6 f4 b4 @6 q) U5 S2 M* k, Q C
VALUES 7 |+ m& x+ Y) z9 C1 n% }6 Z% S
(1, 1)& l! p* U' _; f
ON DUPLICATE KEY UPDATE counter_elem = counter_elem+1;# m) @: y) [$ w5 Q! _9 B3 c7 Q
对于多行:
- M5 v" w: R3 ?/ JINSERT INTO table 1 N0 v0 N* h, I
(a, counter_elem)
, [) N5 n- q3 V) G) X, y* JVALUES , ~. `1 z' J0 @. Y
(1, 1),
) P( V* i% E# t" L- r7 V (2, 1)
. U( `$ g) R C1 aON DUPLICATE KEY UPDATE counter_elem = ?;
! H3 e) g! l' a3 M9 x* ?这不起作用:
; g! }7 r7 f0 z) qcounter_elem = VALUES(counter_elem)+1) `% X3 s: R$ E
8 T" c) b6 s ]0 R
解决方案:+ ^7 M4 E4 h% f" x% y. l1 z8 {* B
4 c, `) W8 G9 C9 f) |2 ]
/ @( M0 c6 h- L7 x/ }7 Q; t+ d% ~6 F" W
完全一样!! i9 C3 Z- O- P: Q
INSERT INTO table 7 p" }: G) e" x* O
(a, counter_elem) ; X7 f* Q! Z" h0 W! |# S2 q$ a
VALUES
1 W8 d: l1 P4 q1 P; y (1, 1),
8 B8 j, q0 [1 Z+ V* _4 b (2, 1)
$ f' l& ?1 Q# U! YON DUPLICATE KEY UPDATE counter_elem = counter_elem + 1;
2 A+ W7 E: A: r; s# a% ]4 m那里没有问题! |
|
|
|
|
|