显示错误代码:1060。在 MySQL 中运行公用表表达式时,列名“column_name”重复

问题描述 投票:0回答:1

有 2 个名为“客户”和“付款”的表,当我在两个表上应用公共表表达式来生成临时表“temp”并从临时表“temp”中搜索任何内容时,显示 1060 错误,提示“列名重复”客户ID' '。 查询如下:

with temp as (
    select *, avg(amount) over(order by c.customer_id) as 'average price till this count',
    count(address_id) over(order by p.customer_id) as "count"
    from payment as p
    inner join customers as c
    on c.customer_id = p.customer_id
)
select concat(first_name, " ", last_name) as 'full name', amount
from temp;

客户表中的列名称是: 客户 ID、名字、姓氏、地址 ID

付款表中的列名称为: customer_id、金额、方式、付款日期。

请提出解决方案并告诉我是否犯了任何错误。 谢谢

我尝试删除额外的查询,仍然收到相同的错误代码 1060 重复列名“customer_id”

with temp as (
    select *
    from payment as p
    inner join customers as c
    on c.customer_id = p.customer_id
)
select first_name, last_name, amount
from temp;
join duplicates mysql-workbench common-table-expression mysql-error-1060
1个回答
0
投票

我猜

SELECT *
从两个表中选择项目会导致什么问题

WITH temp AS (
  SELECT 
    c.customer_id AS c_customer_id, 
    p.customer_id AS p_customer_id, 
    -- include other columns you need here
    -- other part of query....;
© www.soinside.com 2019 - 2024. All rights reserved.