英文:
Fix duplicate column name error on an SQL LEFT JOIN
问题
SQL任务使用IMDB数据库。我想将连接保存为新表格。它总是在MySQL Workbench上引发错误。
DROP TABLE dir_movie_join;
CREATE TABLE dir_movie_join
(
SELECT t1.*, t2.*
FROM directors t1
LEFT JOIN movies t2 ON t1.index = t2.index
);
错误代码:1060. 重复的列名 'index'
我该如何解决这个问题?
英文:
My SQL assignment uses the IMDB database. I want to save joins as a new table. It always raises an error on MySQL Workbench.
DROP TABLE dir_movie_join;
CREATE TABLE dir_movie_join
(
SELECT t1.*, t2.*
FROM directors t1
LEFT JOIN movies t2 ON t1.index = t2.index
);
> Error Code: 1060. Duplicate column name 'index'
How do I navigate this?
答案1
得分: 1
而不是使用 .*
,请只指定必要的列。在这样做时,您必须决定在新表中包含哪个“索引”。如果值始终相同,请选择其中一个并留下另一个。如果它们不同,请通过别名为其中一个指定不同的名称。
CREATE TABLE
通常仅在启动项目时执行。这不是日常任务。
从几个表中获取数据的SELECT
可以包括JOIN
;但通常在SELECT
期间完成,而不创建任何新表。
英文:
Instead of .*
, specify just the necessary columns. In doing so, you must decide which "index" to include in the new table. If the values are always the same, pick either and leave the other behind. If they are different, give one of them a different name via aliasing.
CREATE TABLE
is usually done only when you start a project. It is not a daily task.
A SELECT
to fetch data from several tables can include a JOIN
; but that is normally done during the SELECT
, without creating any new tables.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论