MySQL左外连接问题双重id [重复]

MySQL左外连接问题双重id [重复]

问题描述:

This question already has an answer here:

I'm using a left outer join in a query. but now I'm getting a double id, What I mean is that I now get the id of table 1 and the id of table2 both called id.

Is there a way to maybe rename(prefix) the id of table 2 with something like table2_id? without having to use 'AS table_nameOfColumn' for every column of table 2

My current query:

SELECT invoices.*, clients.* FROM invoices 
LEFT OUTER JOIN users ON invoices.employee_id = users.id 
LEFT OUTER JOIN clients ON users.client_id = clients.id
WHERE invoices.employee_id = 3
</div>

此问题已经存在 这里有一个答案: p>

  • MySQL:左连接和不同表中具有相同名称的列? 6 answers span> li> ul> div>

    我' m在查询中使用左外连接。 但现在我得到一个双重身份, 我的意思是我现在得到表1的id和table2的id都叫id。 p>

    有没有办法可能 用 table2_id之类的东西重命名(前缀)表2的id? 无需为表2的每一列使用“AS table_nameOfColumn” p>

    我当前的查询: p>

      SELECT发票。*,客户。  * FROM发票
    LEFT OUTER JOIN用户ON invoices.employee_id = users.id 
    LEFT OUTER JOIN客户端ON users.client_id = clients.id 
    WHERE invoices.employee_id = 3 
      code>  pre> 
       DIV>

you can simply add an alias to your columns name naming explicitally

eg:

  SELECT invoices.id as invoce_id, clients.id as client_id FROM invoices 
  LEFT OUTER JOIN users ON invoices.employee_id = users.id 
  LEFT OUTER JOIN clients ON users.client_id = clients.id
  WHERE invoices.employee_id = 3