2012-11-12 90 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