列はnull許容ではないため、0
を渡す必要があります s null
の代わりに ■INSERT
に値を指定したくない列の場合 ステートメントと0
との比較を使用します null
の代わりに :
INSERT INTO stock (price, stock1, stock2) VALUES
('99', '10', 0),
('120', 0, '12')
ON DUPLICATE KEY UPDATE
`stock1-C` = `stock1-C` + (VALUES(stock1) <> 0 AND stock1 <> 0),
stock1 = stock1 + VALUES(stock1),
`stock2-C` = `stock2-C` + (VALUES(stock2) <> 0 AND stock2 <> 0),
stock2 = stock2 + VALUES(stock2)
結果:
price | stock1 | stock1-C | stock2 | stock2-C |
---|---|---|---|---|
99 | 10 | 0 | 0 | 0 |
120 | 0 | 0 | 12 | 0 |
それからまた:
INSERT INTO stock (price, stock1, stock2) VALUES
('99', '10', 0),
('120', 0, '12')
ON DUPLICATE KEY UPDATE
`stock1-C` = `stock1-C` + (VALUES(stock1) <> 0 AND stock1 <> 0),
stock1 = stock1 + VALUES(stock1),
`stock2-C` = `stock2-C` + (VALUES(stock2) <> 0 AND stock2 <> 0),
stock2 = stock2 + VALUES(stock2)
結果:
price | stock1 | stock1-C | stock2 | stock2-C |
---|---|---|---|---|
99 | 20 | 1 | 0 | 0 |
120 | 0 | 0 | 24 | 1 |
次に:
INSERT INTO stock (price, stock1, stock2) VALUES
('150', '55', 0),
('150', 0, '56')
ON DUPLICATE KEY UPDATE
`stock1-C` = `stock1-C` + (VALUES(stock1) <> 0 AND stock1 <> 0),
stock1 = stock1 + VALUES(stock1),
`stock2-C` = `stock2-C` + (VALUES(stock2) <> 0 AND stock2 <> 0),
stock2 = stock2 + VALUES(stock2)
結果:
price | stock1 | stock1-C | stock2 | stock2-C |
---|---|---|---|---|
99 | 20 | 1 | 0 | 0 |
120 | 0 | 0 | 24 | 1 |
150 | 55 | 0 | 56 | 0 |
次に:
INSERT INTO stock (price, stock1, stock2) VALUES
('150', '10', 0),
('150', 0, '10')
ON DUPLICATE KEY UPDATE
`stock1-C` = `stock1-C` + (VALUES(stock1) <> 0 AND stock1 <> 0),
stock1 = stock1 + VALUES(stock1),
`stock2-C` = `stock2-C` + (VALUES(stock2) <> 0 AND stock2 <> 0),
stock2 = stock2 + VALUES(stock2)
結果:
price | stock1 | stock1-C | stock2 | stock2-C |
---|---|---|---|---|
99 | 20 | 1 | 0 | 0 |
120 | 0 | 0 | 24 | 1 |
150 | 65 | 1 | 66 | 1 |
デモ
を参照してください。 。