0

I have three tables:

STUDENT table:

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

COURSE table:

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 table:

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)
);

I wrote this query:

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;

To get expect output table like this:

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

but I get an error:

Column sid in field list is ambiguous

Please someone correct my query.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
MY PC
  • 151
  • 2
  • 12
  • 2
    Possible duplicate of [1052: Column 'id' in field list is ambiguous](https://stackoverflow.com/questions/6638520/1052-column-id-in-field-list-is-ambiguous) – MrTux Feb 07 '18 at 11:18
  • As error indicated, 'sid' is there multiple tables. Use table name with column name – Mittal Patel Feb 07 '18 at 11:21
  • select sid as studentid,fname,lname,cid as courseid,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; still getting that error @Mittal – MY PC Feb 07 '18 at 11:24
  • `and sid=1` You must put an alias for that condition – manuerumx Feb 07 '18 at 11:25

1 Answers1

1

You need to add alise as below. Also, use the join instead of adding all the tables in 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;
Mittal Patel
  • 2,732
  • 14
  • 23
  • 1
    In your query you have used ' select sid....', you have sid column in all three table, in such cases, you must need to provide the table name/table alias name along with the table... like "select student.sid ..." Whereve you are using it ... in select, where... – Mittal Patel Feb 07 '18 at 11:30