Mysql重复错误

时间:2016-08-02 09:20:16

标签: mysql

我已经写了一个如下所示的查询,以便插入唯一的' project_title'表中的值('项目')。

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

这项工作正常,直到输入项目标题和项目描述的相同条目,并且查询将如下:

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

现在,我收到了这个错误:

[Err] 1060 - Duplicate column name 'text'

如何摆脱这种重复错误?!

1 个答案:

答案 0 :(得分:1)

在此处提取错误的语句:

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

此查询将生成此错误

  

[Err] 1060 - 重复列名称'文字'

因为您没有为列提供任何别名。

<强>解决方案:

为列添加别名:

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