简体   繁体   English

插入或更新(如果存在)到mysql表中

[英]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. 在这种情况下,所有用户的产品ID均相同。 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. 例如,产品ID-用户1、2、3 ...的123...。我想导入更改了产品价格的CSV文件,或者它可能是特定用户的新产品。

In this case product should be insert if not present for particular user and should update if already exists. 在这种情况下,如果特定用户不存在产品,则应插入产品;如果已经存在,则应进行更新。

Myql query Myql查询

  • insert into table_name (prodid, uid, price) values('123',10, 100) on duplicate key 在重复键上插入table_name(prodid,uid,price)值('123',10,100)

  • update price = IF(uid = values(uid), values(price), price) 更新价格= IF(uid =值(uid),值(price),价格)

In this - added unique constraints for - prodid. 在此-为-prodid添加了唯一的约束。

Pls. help me. 帮我。 Thanks. 谢谢。

  1. change your 'uid' to unique 'screenshoot unique in phpMyAdmin' 将您的“ uid”更改为唯一的“在phpMyAdmin中唯一的屏幕截图”

  2. try this query : 'screenshoot query in phpMyAdmin' 尝试以下查询: '在phpMyAdmin中查询查询'

insert into table_name (prodid, uid, price) values('123',2, 100) on duplicate key update price = values(price) 在重复键更新价格=值(price)上插入table_name(prodid,uid,price)值('123',2,100)

Happy coding.. 快乐编码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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