INSERT INTO..SELECT..ON DUPLICATE KEYS 不明确的 ID

发布于 2024-08-01 12:43:26 字数 2877 浏览 3 评论 0原文

我有下表:

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 可以识别该列?

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天荒地未老 2024-08-08 12:43:26

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文