0

Let's assume we have this table:

--------------------------
| x  |  y  |  z  |  data  |
---------------------------
| 3  |  53 |  24 |  hello |
---------------------------

-

Now I only want to update "data" in case there is the exact combination of X, Y, Z.

INSERT INTO TABLE SET x=?,y=?,z=?,data=? ON DUPLICATE KEY UPDATE data=?

This obviously doesn't work. How would I do this?

Bob
  • 1,201
  • 1
  • 9
  • 22

1 Answers1

2

You only add a composite unique key over the three fields x,y,z. the it works.

You can also use this syntax:

INSERT INTO TABLE (x,y,z,data) values (?,?,?,?) ON DUPLICATE KEY UPDATE data=?;
Bernd Buffen
  • 14,525
  • 2
  • 24
  • 39
  • Thanks for the answer :) You could have been more clear for "composite unique key" but I found it quickly by searching. – Bob Feb 13 '16 at 23:13