我正在写一个与MySQL数据库交互的应用程序。多表SQL搜索查询
我有3个表格,'类别','书籍'和'book_category'。
/* stores properties for categories */
`categories` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(140) NOT NULL UNIQUE,
PRIMARY KEY (`id`)
);
/* stores properties for books */
`books` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` int(11) NOT NULL,
`read` tinyint(1) NOT NULL default 0,
`creation_date` timestamp NOT NULL default CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
);
/* matches books against categories */
`book_category` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`category_id` int(11) NOT NULL,
`item_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
);
我有2个变量来执行搜索。
<?php
$categoryString = 'python'; // books saved under python
$readBool = 1; // all books that have been read
?>
我还想写下面的SQL查询:
选择已阅读所有书籍蟒(即,book.read = 1)。
我的SQL知识非常有限,请大家帮忙。
in book_category table item_id是否为外部的book表id? – 2013-04-09 10:04:59
看起来像book_category.category_ID = categories.id和book_category.item_id = books.id所以一个简单的连接或2应该这样做:) – Dave 2013-04-09 10:06:02
这是功课吗? – 2013-04-09 10:18:05