开发者

Mysql - Insert based on other table where condition - ON DUPLICATE KEY UPDATE

开发者 https://www.devze.com 2023-02-26 08:31 出处:网络
UPDATE I found the answer and provided below Dear All, I want to insert into a table 1) Based on condition on other table

UPDATE

I found the answer and provided below

Dear All,

I want to insert into a table 1) Based on condition on other table 2) and using ON DUPLICATE KEY UPDATE on the first table.

The following query I wrote which is syntactically wrong . Could you please help me on the correct query for this ?

INSERT INTO my_all_count (type,code,count) values ( 0,1,1) 
ON DUPLICATE KEY UPDATE count = count + 1 
WHERE NOT EXISTS ( 
  select 1 from my_reg_count 
  where country_code=CurrCountry and type=0 and code=0);

here 1) I want to insert into table my_all_count 2) type,code is a ke开发者_JAVA百科y and if it exists increasing county by 1 3) Insert only when it is not exists in my_reg_count

Thanks for your help

Regards

Kiran


I found the solution and here is the answer

INSERT INTO my_all_count (type,code,count) 
    select 0,0,1 from dual WHERE NOT EXISTS ( 
         select * from my_reg_count where country_code=CurrCountry 
          and type=0 and code=0) ON DUPLICATE KEY UPDATE count = count + 1;


Not sure this will work, but I am sure the ON DUPLICATE KEY clause should be after the WHERE clause:

INSERT INTO my_all_count (type,code,count) values (0,1,1) 
WHERE NOT EXISTS ( 
  select 1 from my_reg_count 
  where country_code=CurrCountry and type=0 and code=0)
ON DUPLICATE KEY UPDATE count = count + 1; 
0

精彩评论

暂无评论...
验证码 换一张
取 消