Note that there are some explanatory texts on larger screens.

plurals
  1. POQuerying SQL multiple tables
    primarykey
    data
    text
    <p>I have a sql database with the following tables</p> <p>Book Table</p> <pre><code>CREATE TABLE IF NOT EXISTS `books` ( `book_id` varchar(8) NOT NULL DEFAULT '', `book_title` varchar(100) DEFAULT NULL, `author1` varchar(20) NOT NULL, `author2` varchar(20) DEFAULT NULL, `publisher` varchar(20) NOT NULL, `pub_year` year(4) NOT NULL, `mod_id` varchar(8) NOT NULL, `courseID` varchar(8) NOT NULL, PRIMARY KEY (`book_id`), KEY `id` (`book_id`), KEY `book_id` (`book_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; </code></pre> <p>Courses Table </p> <pre><code>CREATE TABLE IF NOT EXISTS `courses` ( `courseID` varchar(8) NOT NULL, `course_title` varchar(255) CHARACTER SET ascii NOT NULL, `Entry_Year` int(1) NOT NULL, `Duration` int(1) NOT NULL, PRIMARY KEY (`courseID`), KEY `courseID` (`courseID`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; </code></pre> <p>Modules Table</p> <pre><code>CREATE TABLE IF NOT EXISTS `modules` ( `mod_id` varchar(8) NOT NULL, `mod_title` varchar(255) NOT NULL, `description` varchar(255) NOT NULL, PRIMARY KEY (`mod_id`), KEY `mod_title` (`mod_title`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; </code></pre> <p>Module Course Table</p> <pre><code>CREATE TABLE IF NOT EXISTS `mod_course` ( `ID` int(11) NOT NULL AUTO_INCREMENT, `module` varchar(8) NOT NULL, `course` varchar(8) NOT NULL, PRIMARY KEY (`ID`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=31 ; </code></pre> <p>I would like to query the database to show all book details of a course. A course has many modules and modules have many books. I have tried the following query but I think there is a problem with my tables as well. (FYI 'BIT' is the id of a course that is in the module course table)</p> <pre><code>SELECT b.book_id, b.book_title, b.author1, b.author2, b.publisher, b.pub_year, b.mod_id, mc.course FROM books b JOIN mod_course mc WHERE mc.course = 'BIT' </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload