2

I get an error of dupicate column name if fromm my query and dont know how to solve my query is

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;    

can any one heplp

10
  • 4
    Table schemas would be helpful. Commented Nov 12, 2012 at 11:49
  • and the result you are getting Commented Nov 12, 2012 at 11:50
  • @BrunoVieira duplicate column name Commented Nov 12, 2012 at 11:50
  • 1
    The error message usually includes the exact position of the error. So please attach the very message you get, while executing this query. Commented Nov 12, 2012 at 11:51
  • 2
    My suggestion would be to review SQL JOIN syntax - here is a great visual explanation of joins Commented Nov 12, 2012 at 11:52

4 Answers 4

7

To avoid duplication prefix the column name with its table as follows:

tab_name.col_name
Sign up to request clarification or add additional context in comments.

2 Comments

+1 Without more details from the asker, anything above this would be guessing
my table structure is like 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 text NOT NULL, createdOn timestamp NULL DEFAULT CURRENT_TIMESTAMP, status tinyint(4) DEFAULT '0', fileName text, PRIMARY KEY (id) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1 |
0

Try writing all column names attach with their respective table names:

Column names without table name attached in your query: … senderid,recipientid,courseid,message …

You should also write above as you are writing these in your query: … mess.id,User.firstName …

Hope this will solve the error.

1 Comment

coloumn name attach with table names doesnt work it still shows same error
0

Try replacing

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

with

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

Happy Coding !

Comments

0

to avoid duplication write query as below using 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.