Mosi Mosi - 4 months ago 9
MySQL Question

Mysql duplication error

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?!

Answer

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;
Comments