How to write the query
I am having one situation, i.e. in one table i am having Course, Semester and University
Columns, in another table i am having Book Number, Author, Booktitle, Publication and Year
. For each course, each semester and each university the book differs(i.e., Boo开发者_开发技巧k Number, Author, Booktitle, Publication
), what i am have to do is based on the course, semester and university i have to retrieve the values of Book Number, Author, Booktitle and Publication
. i.e. i am doing B.E. Mech, 2nd year means, the retrieving values of Book Number, Author, Booktitle and Publication should be corresponding to B.E. Mechanical, 2nd year books.
From what I understand, there may be more than one Course / Semester / University combination for one book and probably more than one book per Course / Semester / University? This is a many-to-many relation and can only be solved by a third table:
CsuToBooks
----------
CsuID
BookID
When this table is filled, use two 'LEFT JOIN' relations in your Queries:
SELECT Course, Semester, University, BookNumber
FROM csu
LEFT JOIN CsuToBooks ON (csu.ID = CsuToBooks.csuID)
LEFT JOIN Books ON (CsuToBooks.BookID = Books.ID)
You have to have a common colum in both tables.For example adding course_id in both tables, so that you can than join both tables by using this common colum
In your first table, add a Book_number column. This will provide a mapping between the 2 tables. Then you can write the query like
Select * from book_table
where book_table.book_number = course.book_number
AND course.course = <course name>
AND course.Semester = <semester>
AND course.university = <university
You'll want a linking table because (presumably) books and courses have a many-to-many relationship. So you'll have Courses_Books with a CourseID and BookID column.
SELECT
booknumber,
author,
booktitle,
publication
FROM
table1,
table2
WHERE
course = 'mech' AND semester = 4
精彩评论