gpt4 book ai didi

php - 查询SQL多表

转载 作者:行者123 更新时间:2023-11-30 01:01:19 24 4
gpt4 key购买 nike

我有一个包含下表的 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;

类(class)表

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;

模块类(class)表

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 ;

我想查询数据库以显示类(class)的所有书籍详细信息。一门类(class)有很多模块,模块有很多书。我已尝试以下查询,但我认为我的表也有问题。 (仅供引用,“BIT”是模块类(class)表中类(class)的 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'

最佳答案

我对此进行了更深入的研究,并提供了以下建议,其中还包括对数据模型的更改。该解决方案还允许您将同一本书用于多个模块(如果将来需要)。另请注意新表 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'

关于php - 查询SQL多表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20105720/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com