0

I have a table with this data

mysql> SELECT * FROM prueba;
+----+------+------+------+
| id | U1   | U2   | cant |
+----+------+------+------+
|  2 | U123 | U124 |    2 |
+----+------+------+------+

I neeed to increment 'cant' in 1 everytime i execute the statement. But if the row doesn't exists it has to be created. The columns U1 and U2 are pairs of data, this means a U1,U2 pair is unique, and thats the condition to search if the record exists.

This is what i have made so far.

select U1,U2,cant,
    (
        case when exists (select U1,U2 from prueba where U1='U123'
        and U2='U124')
            then 1
            else 0
        end
    ) as tmp

    from prueba
pggx999
  • 1
  • 1

1 Answers1

0

See the accepted answer in insert-to-table-or-update-if-exists-mysql. The idea is to use MySQLs

INSERT INTO ... ON DUPLICATE KEY UPDATE ...
Community
  • 1
  • 1
Benvorth
  • 6,363
  • 7
  • 40
  • 64