本文介绍了mysql中JOIN上的重复列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此sql有问题
SELECT COUNT( * )
FROM (
SELECT *
FROM `user` `t`
JOIN `user_relation` r ON ( t.user_id = r.follower_id
OR t.user_id = r.user_id )
WHERE r.status = "active"
AND (
r.user_id =125
OR r.follower_id =125
)
AND t.user_id !=125
GROUP BY t.username
)sq
我总是收到错误消息:#1060-重复的列名'user_id'"任何人都可以帮忙/解释一下,我做错了什么吗?
I always get an error: "#1060 - Duplicate column name 'user_id' " Can anyone help/explain, what I did wrong?
先谢谢了扬
推荐答案
您需要在内部查询中提供别名列
You need to provide aliased column in inner query
SELECT COUNT( * )
FROM (
SELECT t.*
FROM `user` `t`
JOIN `user_relation` r ON ( t.user_id = r.follower_id
OR t.user_id = r.user_id )
WHERE r.status = "active"
AND (
r.user_id =125
OR r.follower_id =125
)
AND t.user_id !=125
GROUP BY t.username
)sq
由于您只对count(*)
感兴趣,因此您只能返回t.*
或r.*
或任一列,条件是内部查询必须中的列名称必须唯一,或者如果在两个表中都相同,但前缀是表别名.
Since you are interested in count(*)
only you can return either t.*
or r.*
or any one column , the condition being that column names in inner query MUST be unique or if are same in both tables than prefixed with table alias name.
这篇关于mysql中JOIN上的重复列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!