修复SQL LEFT JOIN中重复列名错误。

huangapple go评论63阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年5月7日 00:42:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76190034.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定