查询出与jack互为好友的人名字

建表

/*
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'));


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值