L
L
LebedevStr2017-05-11 10:00:54
MySQL
LebedevStr, 2017-05-11 10:00:54

How to implement such an update in the SQL database?

Hello. There is such structure:
ainc |id |price |dost |name |field1 |field2 |field3
1 |78 |232 |1 |qwer |jdfglh |value2 |rghtr
2 |26 |139 |0 |dfwq |hutrh |value2 |gr46g
3 |99 |656 |1 |efew |rhier |value2 |fgdfg
4 |85 |134 |1 |43fd |ergui |value2 |xzvfg
5 |59 |614 |0 |vcrw |hjfue |value2 |dgrhj
ainc - auto_increment
id - primary key
I fill the database with the following query
INSERT INTO db (id,price,dost,name,field1,field2,field3) VALUES ('65','659', '1','qwery','dfsdd','rgers',' zxcdf')
Due to the fact that the table has a PRIMARY KEY, records with a NOT unique ID field do not get into the database Duplicate entry - Error
But is it possible to update some fields,
There is a database query, SQL sees that the query contains a duplicate - that is, the ID is already in the database, and UPDATES the row, for example, according to the price and fild3 columns for the duplicate row (ID).
Many thanks for your help in solving the problem.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Eremin, 2017-05-11
@EreminD

insert into t1(a, b, c)
select d, e, f from t2
on duplicate key update b = e, c = f;

https://dev.mysql.com/doc/refman/5.7/en/insert-on-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question