2012-11-12 89 views
2

我得到,如果弗洛姆我查询的dupicate列名的错误和不知道如何解决我的查询是重复的列名错误

SELECT distinct(courseid+senderid+recipientid) as diskey, 
     mess.id, 
     User.firstName as recipientName, 
     Course.name, 
     senderid, 
     recipientid, 
     courseid, 
     message, 
     mess.status, 
     mess.createdOn 
FROM 
(
    SELECT * 
    from Message,User,Course 
    where Message.recipientid=User.id 
     and Message.courseid=Course.id 
    order by Message.createdOn DESC 
) as mess, 
    User, 
    Course 
WHERE senderid ='3' 
     OR recipientid='3' 
GROUP BY diskey;  

任何一个可以heplp

+4

表的模式将是有益的。 –

+0

和你得到的结果 –

+0

@BrunoVieira重复列名 –

回答

7

为避免重复前缀其表的列名如下:

tab_name.col_name 
+2

+1没有从提问者的更多细节,以上任何东西都是猜测 –

+0

我的表结构就像Message | CREATE TABLE'Message'( 'id' int(11)NOT NULL AUTO_INCREMENT, 'courseid' int(11)NOT NULL, 'senderid' int(11)NOT NULL, 'recipientid' int(11)DEFAULT NULL , 'message'文本NOT NULL, 'createdOn'时间戳NULL DEFAULT CURRENT_TIMESTAMP, 'status' TINYINT(4)DEFAULT '0', 'fileName'文本, PRIMARY KEY('id') )ENGINE = InnoDB的AUTO_INCREMENT = 2 DEFAULT CHARSET = latin1 | – am123

0

尝试写所有列名各自的表名附加:

无表名,列名附加在您的查询: … senderid,recipientid,courseid,message …

你也应该写上面,你在你的查询写这些: … mess.id,User.firstName …

希望这将解决该错误。

+0

coloumn名称附上表名不工作它仍然显示相同的错误 – am123

0

尝试用

HAVING senderid ='3' 
    OR recipientid='3' 

编码快乐更换

WHERE senderid ='3' 
    OR recipientid='3' 

0

避免重复写入查询,如下使用前缀(表名).columnname:

select id, name1,name2 from(
select table1.id,table1.name as name1, table2.name as name2 from 
table1 
innerjoin table2 on table1.id=table2.table1_id 
)group by id