mysql查询中的重复列名错误

时间:2012-11-12 11:47:34

标签: mysql

如果从我的查询中获取并且不知道如何解决我的查询,则会出现重复列名的错误

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;    

任何人都可以提供帮助

4 个答案:

答案 0 :(得分:7)

为避免重复为列名添加前缀及其表,如下所示:

tab_name.col_name

答案 1 :(得分:0)

尝试使用各自的表名写下所有列名:

查询中未附加表名的列名: … senderid,recipientid,courseid,message …

您在编写查询时也应该写上面的内容: … mess.id,User.firstName …

希望这能解决错误。

答案 2 :(得分:0)

尝试替换

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

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

快乐的编码!

答案 3 :(得分:0)

使用prefix(tableName).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