views:

281

answers:

1

Hey there,

I have the following table:

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)

There is one unique key(promoter, generation, payed):

+---------------------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| 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)

Now I want to mark every earning for a promoter as paid by updating the same entry with paid=1 (if it exists).

So if I wanted to mark the earnings of promoter 4 as paid this is what the table should look like:

+----+----------+------------+----------+-------+
| 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)

This is my current approach(without the DELETE which is trivial):

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;

But mysql keeps telling me that turnover is ambiguous:

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

Does anybody have a hint as I can't alias the table that I'm inserting to.

How can I give the table I'm inserting to a name so that mysql can identify the column?

Thanks in advance.

+3  A: 

you have a turnover field in both tables, so mysql can't decide which one you mean at the last row.

Zed
Yes I know that..that's exactly my problem.The Question is:How can I give the table I'm inserting a name so that mysql can identify the column?
André Hoffmann
wouldn't this work?ON DUPLICATE KEY UPDATE bright_promotion_earnings.turnover = bright_promotion_earnings.turnover + commission.turnover2;
Zed
Yes that worked thank you very much.
André Hoffmann