INNER JOIN语法错误

时间:2015-03-05 19:03:53

标签: mysql

我收到以下错误:

"Couldn't execute query. Duplicate column name 'id'"

使用此SQL语句时:

SELECT count(*) as c FROM (
    SELECT *, CONCAT_WS(', ', subordinates.last_name, subordinates.first_name) AS subordinate_name 
    FROM orders 
    INNER JOIN subordinates ON orders.subordinate = subordinates.id WHERE orders.responsible = 8)

两个表(“下属”和“订单”)都有一个“id”列。加入“下属”表时,我可以排除“id”列吗?

我正在使用MySQL

2 个答案:

答案 0 :(得分:-1)

将查询更改为

SELECT count(*) as c 
FROM    (
        SELECT orders .*, CONCAT_WS(', ', subordinates.last_name, subordinates.first_name) AS subordinate_name 
        FROM orders INNER JOIN subordinates ON orders.subordinate = subordinates.id 
        WHERE orders.responsible = 8
    )

确保您在子选择中只有一个Id

您只需要确保您在子选择中没有重复的名称。

另外,一般的经验法则是不使用SELECT *,因为它可以极大地影响性能

答案 1 :(得分:-3)

SELECT count(*) as c 
FROM (
    SELECT orders.*, 
        CONCAT_WS(', ', subordinates.last_name, subordinates.first_name) AS subordinate_name 
    FROM orders 
    INNER JOIN subordinates 
    ON orders.subordinate = subordinates.id 
    WHERE orders.responsible = 8)