简体   繁体   中英

Insert or update if exists into mysql table

Table structure.

id, uid, prodid, price
1    1     123   10 
2    2     123   20  
3    3     123   40

In this case product id are same for all users. eg prod id - 123 for user 1, 2, 3 .... I want to import csv file having product price changed or it may be new product for particular user.

In this case product should be insert if not present for particular user and should update if already exists.

Myql query

  • insert into table_name (prodid, uid, price) values('123',10, 100) on duplicate key

  • update price = IF(uid = values(uid), values(price), price)

In this - added unique constraints for - prodid.

Pls. help me. Thanks.

  1. change your 'uid' to unique 'screenshoot unique in phpMyAdmin'

  2. try this query : 'screenshoot query in phpMyAdmin'

insert into table_name (prodid, uid, price) values('123',2, 100) on duplicate key update price = values(price)

Happy coding..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM