两个表中具有相同名称的mysql额外列

时间:2011-01-07 13:47:32

标签: mysql sql join

表1包含列:entry_id user_id ... 表2包含列:entry_id user_id ...

user_id条目并不总是相同,所以我想提取它们,以便稍后我可以在我的脚本中对它们进行比较

SELECT * FROM 
table1 as t1
INNER JOIN table2 as t2 on t1.entry_id=t2.entry_id
WHERE t1.user_id='%s'

我想提取t1.user_id和t2.user_id ......问题是结果数组只有user_id

谢谢

3 个答案:

答案 0 :(得分:4)

使用AS关键字:

SELECT
  t1.user_id as t1_user_id
  ,t2.user_id as t2_user_id
FROM table1 as t1
INNER JOIN table2 as t2
  ON t1.entry_id=t2.entry_id
WHERE t1.user_id='%s'

答案 1 :(得分:0)

SELECT t1.user_id AS user_id_1,t2.user_id AS user_id2,...

我认为执行查询的框架在这​​里“合并”两列是错误的,结果集应该都有,但这些都存储在沿线的某个关联数组中。

最好只请求您实际需要的那些列:)

答案 2 :(得分:0)

您可以有效地使用ALIASING(即使用AS关键字)。

SELECT t1.user_id as user_id_1, t2.user_id as user_id_2  FROM 
table1 as t1
INNER JOIN table2 as t2 on t1.entry_id=t2.entry_id
WHERE t1.user_id='%s'