English 中文(简体)
插入......关于重复的关键
原标题:incremental update in insert ... on duplicate key

Is there any way to do an incremental update in an on duplicate key update insert in mysql?

例重/错误:

insert into blah (user_id, prefix, email_id, field, source) 
select user_id, substring(name, 1, 3), contact_email_id, 2, source from address_book 
on duplicate key update source = source + values(source);

ERROR 1052 (23000): Column  source  in field list is ambiguous
最佳回答

NO, you just cannot specify source = source + values(source);
because source table blah is not included in the select, using alias won t fix.

工作将使用<条码>。

insert into blah (user_id, prefix, email_id, field, source) 
select 
  ab1.user_id, substring(ab1.name, 1, 3), ab1.contact_email_id, 2, 
  if(ab2.source, ab1.source+ab2.source, ab1.source)
from 
  address_book ab1
left join
  blah1 as ab2
on 
  ab1.user_id=ab2.user_id
on duplicate key 
update source = values(source); 
/* values(source) = ab1.source+ab2.source if duplicate found */

<代码>1:N 关系

问题回答

如果你把表格放在一栏名上,即:blah.source,地址是:book.source?





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签