K
K
Kirill Legend2020-08-22 16:33:08
SQL
Kirill Legend, 2020-08-22 16:33:08

Sql. How to update some fields in a table with some user id (not ai), and if there is no field, then create a new record?

Sql. How to update some fields in the table with a certain user id (not ai), and if there is no field with such id, then create a new record?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Ruslan., 2020-08-22
@thelegend3553

It depends on the Sql dialect, you can try this:

update your_table
   set your_field =your_new_value
  from your_table
 where id = your_id

if @@rowcount = 0
insert your_table(id, your_field) 
values (your_id, your_new_value)

D
Dmitry, 2020-08-22
@gebrak

UseMySQL INSERT ON DUPLICATE KEY UPDATE

INSERT INTO table (column_list)
VALUES (value_list)
ON DUPLICATE KEY UPDATE
   c1 = v1, 
   c2 = v2,
   ...;

https://www.mysqltutorial.org/mysql-insert-or-upda...
PS. For sqlite use https://www.sqlitetutorial.net/sqlite-replace-stat...REPLACE INTO table

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question