来自同一个表的mysql多连接给出了别名

时间:2014-06-01 17:32:38

标签: mysql

目前我有以下查询

SELECT * FROM tabs 
JOIN users d ON tabs.`debit` = d.id 
JOIN users c ON tabs.`credit` = c.id 

因为表包含两个用户对象,所返回的名称是相同的:

id | amount | type | id | username | avatar | id | username | avatar

我需要它返回如下

 id | amount | type | debit.id | debit.username | debit.avatar | credit.id | credit.username | credit.avatar

或类似的东西,只要用户的列名称是前缀。

1 个答案:

答案 0 :(得分:1)

我认为这就是你要找的东西。试试看。 (假设id | amount | type属于tabs表)

SELECT t.id,
t.amount,
t.type,
d.id as 'debit.id',
d.username as 'debit.username',
d.avatar as 'debit.avatar',
c.id as 'credit.id',
c.username as 'credit.username',
c.avatar as 'credit.avatar',
FROM tabs t
JOIN users d ON t.`debit` = d.id 
JOIN users c ON t.`credit` = c.id