Bug #8147 a column is proclaimed ambiguous in INSERT ... SELECT .. ON DUPLICATE
Submitted: 26 Jan 2005 17:22 Modified: 27 Jun 2005 10:30
Reporter: Sinisa Milivojevic Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server Severity:S2 (Serious)
Version:4.1 OS:Any (any)
Assigned to: Antony Curtis CPU Architecture:Any

[26 Jan 2005 17:22] Sinisa Milivojevic
Description:
There is the error in declaring imp column ambigous, although it does exist only in one of the tables.

How to repeat:
  INSERT /*!LOW_PRIORITY*/ INTO vcl_200501_qrym (net_id, query, acct_id,
imp, cts, bid__min_cts, bid__max_cts, bid__sum_cts, rank__sum_cts)
  SELECT net_id, query, acct_id, imp, cts, bid__min_cts, bid__max_cts,
bid__sum_cts, rank__sum_cts
  FROM vclog_day_tmp_20050101_qrym
  ON DUPLICATE KEY UPDATE
        imp = imp + VALUES(imp),
        cts = cts + VALUES(cts),
        bid__min_cts  =    LEAST(bid__min_cts, VALUES(bid__min_cts)),
        bid__max_cts  = GREATEST(bid__max_cts, VALUES(bid__max_cts)),
        bid__sum_cts  = bid__sum_cts   +  VALUES(bid__sum_cts),
        rank__sum_cts = rank__sum_cts  +  VALUES(bid__sum_cts)

DBD::mysql::db do failed: Column 'imp' in field list is ambiguous

also ...

 INSERT /*!LOW_PRIORITY*/ INTO vcl_200501_qrym (net_id, query, acct_id, 
 imp, cts, bid__min_cts, bid__max_cts, bid__sum_cts, rank__sum_cts) 
 SELECT X.net_id A, X.query B, X.acct_id C, X.imp D, X.cts E, 
 X.bid__min_cts F, X.bid__max_cts G, X.bid__sum_cts H, X.rank__sum_cts I
    FROM vclog_day_tmp_20050101_qrym X
    ON DUPLICATE KEY UPDATE
          imp = imp + VALUES(imp),
          cts = cts + VALUES(cts),
          bid__min_cts  =    LEAST(bid__min_cts, VALUES(bid__min_cts)),
          bid__max_cts  = GREATEST(bid__max_cts, VALUES(bid__max_cts)),
          bid__sum_cts  = bid__sum_cts   +  VALUES(bid__sum_cts),
          rank__sum_cts = rank__sum_cts  +  VALUES(bid__sum_cts)

Gave the same error.
[9 Feb 2005 5:30] Antony Curtis
New patch pending
  bk commit - 4.1 tree (acurtis:1.2160) BUG#8147
[17 Feb 2005 22:02] Antony Curtis
Thank you for your bug report. This issue has been committed to our
source repository of that product and will be incorporated into the
next release.

If necessary, you can access the source repository and build the latest
available version, including the bugfix, yourself. More information 
about accessing the source trees is available at
    http://www.mysql.com/doc/en/Installing_source_tree.html

Additional info:

Fix pushed to 4.1 and 5.0 repositories
[22 Feb 2005 19:59] Sergei Golubchik
fixed in 4.1.11
[7 Apr 2005 0:06] Kris Couck
Repeating the test case given in http://bugs.mysql.com/bug.php?id=8732 (marked duplicate of this one), this bug still occurs in mysql 4.1.11
[21 Apr 2005 1:02] Paul Dubois
Noted in 4.1.11 changelog.
[13 May 2005 10:13] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/internals/24847
[29 May 2005 16:24] Kris Couck
Still in 4.1.12 (I don't know if it was pushed in time for 4.1.12's release...)
Didn't show up in the .13 changelogs till now as well though
[17 Jun 2005 21:02] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/internals/26141
[21 Jun 2005 21:58] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/internals/26265
[27 Jun 2005 10:04] Michael Widenius
The original bug here is that the column imp is not uniquely identified as the column exists both in vcl_200501_qrym and in vclog_day_tmp_20050101_qrym and one can refer to both tables in the ON DUPLICATE KEY part.

Fix:
- Use table qualifiers when refering to not unique names in ON DUPLICATE KEY

This patch will be reverted & rewritten in 4.1.13.
[21 Sep 2005 13:30] [ name withheld ]
NSERT INTO customer_daily_spending
                           ( customer_id, spending_day_utc,
                             spending_day_tz, timezone, amount,
                             transaction_count )
                 SELECT t.customer_id, t.pay_date,
                        date(convert_tz(t.pay_date,'UTC',c.timezone)),
                        c.timezone, t.amount, 1
                   FROM transaction t,
                        customer c
                  WHERE t.customer_id = c.id
                    AND t.transcode = 2005092110100000005
                    AND t.is_paid = 'Y'
                 ON DUPLICATE KEY UPDATE amount = customer_daily_spending.amount + VALUES(amount),
                                  transaction_count = transaction_count + 1

ERROR 1052 (23000): Column 'amount' in field list is ambiguous

Caused by VALUES(amount), but this should be clearly defined.