zoukankan      html  css  js  c++  java
  • [遇见时光]2016网易笔试-查询互为好友

    建表

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

    要求Id字段自增长,level表示用户等级;

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

    记录中(jack,tom)表示tom加jack为好友,jack未加tom为好友;存在(tom,jack)(jack,tom)则jack和tom互为好友;

    要求查询与jack互为好友的name并按level从大到小排序:

    #互为好友
    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')) order by level desc;
  • 相关阅读:
    js对话框弹窗
    MySQL-查询数据(SELECT)
    Jmeter分布式压测及踩坑记录
    性能测试基础
    Jmeter响应中中文乱码解决办法
    Jmeter录制脚本
    jmeter命令行运行(非GUI形式)参数详解
    Jmeter关联
    Jmeter之事务控制器
    Jmeter之集合点
  • 原文地址:https://www.cnblogs.com/yujianshiguang/p/5310213.html
Copyright © 2011-2022 走看看