查询SQL多个表

时间:2013-11-20 19:58:47

标签: php mysql sql database

我有一个包含以下表格的SQL数据库

书桌

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;

课程表

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;

模块表

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;

模块课程表

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 ;

我想查询数据库以显示课程的所有书籍详细信息。课程有很多模块,模块有很多书。我尝试了以下查询,但我认为我的表也存在问题。 (仅供参考'BIT'是模块课程表中课程的ID)

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'

3 个答案:

答案 0 :(得分:0)

在关键字加入后,它应该是ON而不是WHERE

SELECT B.*, MC.COURSE
FROM MODULECOURSE AS MC
INNER JOIN COURSES AS C
ON C.COURSE_TITLE = MC.COURSE
INNER JOIN BOOKS AS B
ON B.COURSEID= C.COURSEID

答案 1 :(得分:0)

根据所述关系课程 - > module->书籍,您应该从books表中删除courseid列。你拥有的modid就是你所需要的。

您的查询问题是缺少连接条件。 在JOIN mod_course mc行下面你应该添加 ON mc.module = b.mod_id

答案 2 :(得分:0)

我已经对此进行了深入研究,并提供了以下建议,其中包括对数据模型的更改。此解决方案还允许您将相同的书用于多个模块(如果将来需要)。另请注意新表mod_books 这个(包括查询)的完整代码如下...希望确定

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,
PRIMARY KEY (book_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS courses
(course_ID 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 (course_ID)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

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)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS mod_books
(mod_id varchar(8) NOT NULL,
 book_id varchar(8) NOT NULL,
 PRIMARY KEY (mod_id,book_id),
 FOREIGN KEY (mod_id) REFERENCES modules (mod_id),
 FOREIGN KEY (book_id) REFERENCES books (book_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS mod_course
(ID int(11) NOT NULL AUTO_INCREMENT,
 mod_id varchar(8) NOT NULL,
 course_ID varchar(8) NOT NULL,
 PRIMARY KEY (ID),
FOREIGN KEY (mod_id) REFERENCES modules (mod_id),
FOREIGN KEY (course_ID) REFERENCES courses (course_ID)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=31 ;

,查询将是

SELECT  b.*
FROM    mod_course mc
INNER JOIN mod_books mb
    ON mb.mod_id = mc.mod_id
    INNER JOIN books b
    ON b.book_id = mb.book_id
WHERE   mc.course_id = 'BIT'