建表
/*
Navicat MySQL Data Transfer
Source Server : connect1
Source Server Version : 50611
Source Host : localhost:3306
Source Database : db_school
Target Server Type : MYSQL
Target Server Version : 50611
File Encoding : 65001
Date: 2016-03-23 10:01:57
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `t_user`
-- ----------------------------
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`level` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of t_user
-- ----------------------------
INSERT INTO `t_user` VALUES ('1', 'jack', '1');
INSERT INTO `t_user` VALUES ('2', 'tom', '1');
INSERT INTO `t_user` VALUES ('3', 'jerry', '2');
INSERT INTO `t_user` VALUES ('4', 'jim', '2');
INSERT INTO `t_user` VALUES ('5', 'lucy', '3');
INSERT INTO `t_user` VALUES ('6', 'john', '2');
/*
Navicat MySQL Data Transfer
Source Server : connect1
Source Server Version : 50611
Source Host : localhost:3306
Source Database : db_school
Target Server Type : MYSQL
Target Server Version : 50611
File Encoding : 65001
Date: 2016-03-23 10:02:04
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `t_friend`
-- ----------------------------
DROP TABLE IF EXISTS `t_friend`;
CREATE TABLE `t_friend` (
`f_id` int(11) NOT NULL AUTO_INCREMENT,
`from_uid` int(11) DEFAULT NULL,
`to_uid` int(11) DEFAULT NULL,
PRIMARY KEY (`f_id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of t_friend
-- ----------------------------
INSERT INTO `t_friend` VALUES ('1', '1', '2');
INSERT INTO `t_friend` VALUES ('2', '1', '3');
INSERT INTO `t_friend` VALUES ('3', '1', '4');
INSERT INTO `t_friend` VALUES ('4', '2', '1');
INSERT INTO `t_friend` VALUES ('5', '2', '3');
INSERT INTO `t_friend` VALUES ('6', '3', '2');
INSERT INTO `t_friend` VALUES ('7', '1', '5');
INSERT INTO `t_friend` VALUES ('8', '5', '1');
#互为好友
select name from t_user where id in (select t1.to_uid from t_friend t1, t_friend t2 where
t1.from_uid=t2.to_uid and t1.to_uid=t2.from_uid and t1.from_uid=(select id from t_user where
t_user.name='jack') and t2.to_uid=(select id from t_user where t_user.name='jack'));