INSERT INTO..SELECT..ON DUPLICATE KEYS模糊ID

时间:2009-08-09 10:55:17

标签: sql mysql insert-update mysql-error-1052

我有下表:

mysql> SELECT *  FROM  `bright_promotion_earnings`;
+----+----------+------------+----------+-------+
| id | promoter | generation | turnover | payed |
+----+----------+------------+----------+-------+
|  1 |        4 |          1 |       10 |     0 | 
|  3 |        4 |          5 |      100 |     0 | 
|  4 |        4 |          3 |    10000 |     1 | 
|  5 |        4 |          3 |      200 |     0 | 
+----+----------+------------+----------+-------+
4 rows in set (0.00 sec)

有一个独特的密钥(发起人,代,付费):

+---------------------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table                     | Non_unique | Key_name   | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+---------------------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| bright_promotion_earnings |          0 | promoter_2 |            1 | promoter    | A         |           2 |     NULL | NULL   | YES  | BTREE      |         | 
| bright_promotion_earnings |          0 | promoter_2 |            2 | generation  | A         |           4 |     NULL | NULL   |      | BTREE      |         | 
| bright_promotion_earnings |          0 | promoter_2 |            3 | payed       | A         |           4 |     NULL | NULL   |      | BTREE      |         | 
+---------------------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
3 rows in set (0.00 sec)

现在,我想通过更新带有pay = 1的相同条目(如果存在)来标记每个获得者的收入。

因此,如果我想将发起人4的收入标记为已付款,那么该表应如下所示:

+----+----------+------------+----------+-------+
| id | promoter | generation | turnover | payed |
+----+----------+------------+----------+-------+
|  4 |        4 |          3 |    10200 |     1 | 
|  6 |        4 |          5 |      100 |     1 | 
|  7 |        4 |          1 |       10 |     1 | 
+----+----------+------------+----------+-------+
3 rows in set (0.00 sec)

这是我目前的方法(没有DELETE这是微不足道的):

INSERT INTO 
    bright_promotion_earnings
    (
        promoter,
        generation,
        turnover,
        payed
    )
SELECT 
    commission.promoter,
    commission.generation,
    commission.turnover as turnover2,
    '1' as payed
FROM  
    bright_promotion_earnings as commission
WHERE  
    promoter=4
    AND payed=0
ON DUPLICATE KEY UPDATE turnover=turnover+turnover2;

但是mysql一直告诉我,营业额不明确:

#1052 - Column 'turnover' in field list is ambiguous

是否有人提示,因为我无法为我要插入的表添加别名。

如何将表格插入到名称中,以便mysql可以识别列?

提前致谢。

1 个答案:

答案 0 :(得分:5)

你在两个表中都有一个营业额字段,所以mysql无法决定你在最后一行的意思。