gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 09:54:12 25 4
gpt4 key购买 nike

我正在尝试将数据库从 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;

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

最佳答案

第一个子查询语法返回两个 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;

您还可以预先定义要在子查询中返回哪一列。示例:

(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

这样,您的外部查询应该根据您在子查询中预定义的内容选择 a.id_document,并且不需要从外部查询中更改任何内容。

关于mysql - "Duplicate column name"不应该存在的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54184781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com