MySQL Query出现意外错误

时间:2018-02-07 11:17:11

标签: mysql

我有三张桌子:

学生表:

create table student
(
    sid int auto_increment primary key, 
    fname varchar(30),
    lname varchar(30)
);

COURSE 表:

create table course
(
    cid int auto_increment primary key, 
    ctype text,
    cclass text,
    cduration int,
    certification int,
    cteacher text,
    standard_course_fee int,
);

STUDENT_PAYMENT 表:

create table student_payment
(
    transaction_id int auto_increment primary key,  
    sid int,
    cid int,
    paidamount int,
    paiddate date,
    paymentdue int,
    FOREIGN KEY (sid) REFERENCES student(sid),
    FOREIGN KEY (cid) REFERENCES course(cid)
);

我写了这个查询:

select 
    sid, fname, lname, cid, ctype, cclass, paidamount, paiddate, paymentdue 
from 
    student, student_payment, course 
where 
    course.cid = student_payment.cid and 
    student.sid = student_payment.sid and 
    sid = 1;

获得期望输出表如下:

|sid| fname | lname | ctype | cclass | paidamount | paiddate | paymentdue |
---------------------------------------------------------------------------

但是我收到了错误:

  

字段列表中的列sid不明确

请有人更正我的问题。

1 个答案:

答案 0 :(得分:1)

您需要添加alise,如下所示。另外,使用连接而不是添加FROM

中的所有表
    select student.sid,fname,lname,course.cid,ctype,cclass,paidamount,paiddate,paymentdue 
    from student
    inner join student_payment on student.sid=student_payment.sid
    inner join course on course.cid=student_payment.cid
    where student.sid=1;