我有下表:
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)
现在,我想通过使用paid=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 可以识别该列?
提前致谢。