"Duplicate column name" 不应该存在的错误

"Duplicate column name" error that shouldn't exist

我正在尝试将数据库从 sqlite 迁移到 mariaDB(我从未使用过),但我似乎无法解决这个问题!

当我尝试 运行 这个时,我总是得到

"#1060 - Duplicate column name 'id_document'"

查询如下:

SELECT
  a.id_document AS id_document,
  id_user_associatedDoc,
  id_user_creator,
  stage,
  title,
  goldStandardAnnotationManual,
  count(content) AS answer_count
FROM
  (SELECT * 
  FROM Document join DocumentProject 
  ON Document.id_document = DocumentProject.id_document 
  WHERE DocumentProject.id_project = 2) a
  LEFT JOIN
  (SELECT * 
  FROM Annotation 
  WHERE Annotation.id_project = 2) b 
  ON a.id_document = b.id_document
GROUP BY a.id_document;

我不明白为什么会出现此错误!你能帮忙吗?

这第一个子查询语法 returns 两个 id_document 列:

(SELECT *
 FROM Document
   join DocumentProject
   ON Document.id_document = DocumentProject.id_document
 WHERE DocumentProject.id_project = 2) a

解决此问题的快速方法:

SELECT
  a.id_doc /*Change this column as following*/ AS id_document,
  id_user_associatedDoc,
  id_user_creator,
  stage,
  title,
  goldStandardAnnotationManual,
  count(content) AS answer_count
FROM
  (SELECT *, Document.id_document as "id_doc" /*define the first id_document column as a different name*/
  FROM Document
    join DocumentProject 
    ON Document.id_document = DocumentProject.id_document 
  WHERE DocumentProject.id_project = 2) a
  LEFT JOIN
  (SELECT * 
  FROM Annotation 
  WHERE Annotation.id_project = 2) b 
    ON a.id_document = b.id_document
GROUP BY a.id_document;

您也可以在子查询中预定义要 return 的列。示例:

(SELECT Document.id_document, Document.column_2, Document.column_3
 FROM Document
   join DocumentProject
   ON Document.id_document = DocumentProject.id_document 
 WHERE DocumentProject.id_project = 2) a

这样,您的外部查询应该 select a.id_document 根据您在子查询中预先定义的内容,无需从外部查询更改任何内容。