Ponieważ kolumny nie dopuszczają wartości null, musisz przekazać 0
s zamiast null
s dla kolumn, w których nie chcesz podawać wartości w INSERT
oświadczenia i użyj porównań do 0
zamiast 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)
Wynik:
cena | magazyn1 | stock1-C | magazyn2 | stock2-C |
---|---|---|---|---|
99 | 10 | 0 | 0 | 0 |
120 | 0 | 0 | 12 | 0 |
I znowu:
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)
Wynik:
cena | magazyn1 | stock1-C | magazyn2 | stock2-C |
---|---|---|---|---|
99 | 20 | 1 | 0 | 0 |
120 | 0 | 0 | 24 | 1 |
Następnie:
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)
Wynik:
cena | magazyn1 | stock1-C | magazyn2 | stock2-C |
---|---|---|---|---|
99 | 20 | 1 | 0 | 0 |
120 | 0 | 0 | 24 | 1 |
150 | 55 | 0 | 56 | 0 |
Następnie:
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)
Wynik:
cena | magazyn1 | stock1-C | magazyn2 | stock2-C |
---|---|---|---|---|
99 | 20 | 1 | 0 | 0 |
120 | 0 | 0 | 24 | 1 |
150 | 65 | 1 | 66 | 1 |
Zobacz demo
.