这是我的书籍表tbl_books
CREATE TABLE IF NOT EXISTS `tbl_books` (
`book_id` int(11) NOT NULL AUTO_INCREMENT,
`book_title` varchar(255) NOT NULL,
`book_author` varchar(255) NOT NULL,
`book_year` varchar(255) NOT NULL,
`book_rack` varchar(255) NOT NULL,
PRIMARY KEY (`book_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
这是我的第二个机架表tbl_rack
CREATE TABLE IF NOT EXISTS `tbl_rack` (
`rack_id` int(11) NOT NULL AUTO_INCREMENT,
`rack_name` varchar(255) NOT NULL,
PRIMARY KEY (`rack_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
我想解决的问题: 我想在表格中显示机架的所有数据 身份证号码此机架中没有书籍? 如何显示属于特定机架的书籍总数??? / 请帮忙
答案 0 :(得分:0)
你可以在你的机架上循环并计算该机架上的每本书(我把它拿到tbl_books.book_rack = tbl_rack.rack_name,你知道如何用pdo连接你的数据库,你的连接叫做$ db)
$rack = $db->query('select distinct rack_name from tbl_racks');
$rack->execute(); $result = $rack->fetch_all('PDO::FETCH_ASSOC');
foreach($result as $r){
$books = $db->query('count(book_id) as num_books from tbl_books where book_rack = :book_rack');
$books->execute(array("book_rack"=>$r['rack_name']));
$b = $books->fetch('PDO::FETCH_ASSOC');
echo $b['num_books'];
}