This question already has answers here:
MySQL: Left join and column with the same name in different tables?
                                
                                    (6个答案)
                                
                        
                                2年前关闭。
            
                    
我在查询中使用左外部联接。但是现在我有一个双重身份证,
我的意思是,我现在得到表1的ID和表2的ID都称为ID。

有没有一种方法可以像这样重命名(前缀)表2的ID
table2_id?不必为表2的每一列使用“ AS table_nameOfColumn”

我当前的查询:

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

最佳答案

您可以简单地在列名中添加别名来明确命名

例如:

  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

关于php - MySQL的左外部联接问题双重ID ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48491230/

10-11 07:45