1

I've wrote a query like below in order to insert unique 'project_title' value in the table ('projects').

INSERT INTO projects (projects.project_title, projects.description) 
SELECT * FROM (SELECT 'a title', 'a description') AS tmp 
WHERE NOT EXISTS (SELECT projects.project_title FROM projects WHERE projects.project_title = 'a title') LIMIT 1

This works fine until a same entry for both project title and project description entered and the query will be like this:

INSERT INTO projects (projects.project_title, projects.description) 
SELECT * FROM (SELECT 'text', 'text') AS tmp 
WHERE NOT EXISTS (SELECT projects.project_title FROM projects WHERE projects.project_title = 'text') LIMIT 1

Now, I get this error:

[Err] 1060 - Duplicate column name 'text'

How to get rid of this duplication error?!

3
  • 2
    try giving alias to the columns ...SELECT * FROM (SELECT 'text' AS firstText, 'text' AS secondText) AS tmp .... Commented Aug 2, 2016 at 9:22
  • Thanks, It solved! Commented Aug 2, 2016 at 9:23
  • 1
    @1000111 make it an answer! \o/ Commented Aug 2, 2016 at 9:51

1 Answer 1

1

Extracting the erroneous statement here:

SELECT 
 tmp.*
FROM 
(
    SELECT 'text', 'text'
) AS tmp;

This query will generate this error

[Err] 1060 - Duplicate column name 'text'

Because you didn't give any alias to columns.

Solution:

Give alias to the columns:

SELECT 
  tmp.*
FROM 
(
    SELECT 
      'text' AS firstText, 
      'text' AS secondText
) AS tmp;
Sign up to request clarification or add additional context in comments.

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.