gpt4 book ai didi

mysql - 重复的列名 MySQL

转载 作者:太空宇宙 更新时间:2023-11-03 10:38:23 24 4
gpt4 key购买 nike

我不应该有这个错误。这是代码

drop database if exists myDB;
CREATE DATABASE IF NOT EXISTS myDB;
USE myDB;

CREATE TABLE teacher(
tId int auto_increment primary key,
fname varchar(50),
lname varchar(50),
sId varchar(50)
);


CREATE TABLE student(
sId varchar(50) not null primary key,
fname varchar(50),
lname varchar(50)
);


ALTER TABLE teacher
ADD foreign key (sId) references student(sId);

insert into student values ('123', 'a', 'aa');
insert into student values ('456', 'b', 'bb');
insert into teacher (fname, lname, sId) values ('t', 'tt', '123');
insert into teacher (fname, lname, sId) values ('x', 'xx', '456');



create or replace view showTeachersAndStudents as
select teacher.fname, teacher.lname, student.sId, student.fname, student.lname
from teacher join student on teacher.sId = student.sId;

错误:错误代码:1060。列名称“fname”重复

最佳答案

您的 View 尝试返回 fnamelname 两次。

为您的 View 添加别名:

create or replace view showTeachersAndStudents as
select teacher.fname fname_teacher,
teacher.lname lnamet_teacher,
student.sId,
student.fname fname_student,
student.lname lname_student
from teacher
join student on teacher.sId = student.sId;

关于mysql - 重复的列名 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43263109/

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