------------------------------------------------联合查询------------------------------------------------------------------
DROP TABLE IF EXISTS `book`;
CREATE TABLE `book` (
`book_id` int(11) NOT NULL COMMENT '书的id',
`book_name` varchar(60) DEFAULT NULL COMMENT '书的名称',
`book_author` varchar(30) DEFAULT NULL COMMENT '书的作者',
PRIMARY KEY (`book_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of book
-- ----------------------------
INSERT INTO `book` VALUES ('1', '三国演义', '罗贯中');
INSERT INTO `book` VALUES ('2', '西游记', '某某');
INSERT INTO `book` VALUES ('3', '红楼梦', 'XX');
---------------上面是表1-------------------------------------------------------
DROP TABLE IF EXISTS `borrower`;
CREATE TABLE `borrower` (
`borrower_id` int(11) NOT NULL,
`book_id` int(11) DEFAULT NULL,
`borrower_name` varchar(30) DEFAULT NULL,
`borrower_time` varchar(60) DEFAULT NULL,
PRIMARY KEY (`borrower_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of borrower
-- ----------------------------
INSERT INTO `borrower` VALUES ('1', '2', '安哥', '2012-05-21');
INSERT INTO `borrower` VALUES ('2', '1', '小李', '2012-05-20');
INSERT INTO `borrower` VALUES ('3', '1', '小黄', '2012-05-25');
INSERT INTO `borrower` VALUES ('4', '3', '安哥', '2012-05-21');
---------------上面是表2--------------------------------------------------------
查询书的id=1 有多少人看过 并把看过的人的信息显示出来
select * from book inner join borrower on (book.book_id=borrower.borrower_id) where borrower.book_id=1
------------------------------------结果是---------------------------------------------------------------------
DROP TABLE IF EXISTS `book`;
CREATE TABLE `book` (
`book_id` int(11) NOT NULL COMMENT '书的id',
`book_name` varchar(60) DEFAULT NULL COMMENT '书的名称',
`book_author` varchar(30) DEFAULT NULL COMMENT '书的作者',
PRIMARY KEY (`book_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of book
-- ----------------------------
INSERT INTO `book` VALUES ('1', '三国演义', '罗贯中');
INSERT INTO `book` VALUES ('2', '西游记', '某某');
INSERT INTO `book` VALUES ('3', '红楼梦', 'XX');
---------------上面是表1-------------------------------------------------------
DROP TABLE IF EXISTS `borrower`;
CREATE TABLE `borrower` (
`borrower_id` int(11) NOT NULL,
`book_id` int(11) DEFAULT NULL,
`borrower_name` varchar(30) DEFAULT NULL,
`borrower_time` varchar(60) DEFAULT NULL,
PRIMARY KEY (`borrower_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of borrower
-- ----------------------------
INSERT INTO `borrower` VALUES ('1', '2', '安哥', '2012-05-21');
INSERT INTO `borrower` VALUES ('2', '1', '小李', '2012-05-20');
INSERT INTO `borrower` VALUES ('3', '1', '小黄', '2012-05-25');
INSERT INTO `borrower` VALUES ('4', '3', '安哥', '2012-05-21');
---------------上面是表2--------------------------------------------------------
查询书的id=1 有多少人看过 并把看过的人的信息显示出来
select * from book inner join borrower on (book.book_id=borrower.borrower_id) where borrower.book_id=1
------------------------------------结果是---------------------------------------------------------------------
2 西游记某某2 1 小李 2012-05-20
-----------------------------------------------------
3 红楼梦XX3 1 小黄 2012-05-25
作者:hraceo 发表于2013-5-23 0:34:51 原文链接
阅读:56 评论:0 查看评论