2017-04-06 150 views
0

我不應該有這個錯誤。 這裏是代碼重複的列名稱MySQL

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」

請你幫忙,非常感謝你。

回答

4

您的看法嘗試返回兩次fnamelname

添加別名到您的視圖:

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; 
+0

作品魅力..感謝一百萬。我錯過了這件事 – Yahya

+1

我很高興能夠幫助。 – McNets