mysql 高级 查询

#查出计算机系的所有学生
select name from tab_stu as s left join tab_yxmes as y on s.yxid=y.id where yxmc='计算机系'
#查出 韩顺平所在的院系信息
select yxmc from tab_stu as s left join tab_yxmes as y on s.yxid=y.id where name='韩顺平'
#3)查出在“行政楼”办公的院系名称。
select yxmc from tab_yxmes where xbdz like "行政楼%";
#4)查出男生女生各多少人。
select sex, count(*) from tab_stu group by sex
#5)查出人数最多的院系信息。
select y.yxmc as c from tab_stu as s left join tab_yxmes as y on s.yxid=y.id GROUP BY s.yxid order by c desc limit 1
#查出跟“罗弟华”同籍贯的所有人
select name from tab_stu where jg=(select jg from tab_stu where name='罗第华')
#8)查出有“河北”人就读的院系信息
select jg as '籍贯',yxmc as '院系名称' from tab_stu as s  left join tab_yxmes as y on s.yxid=y.id where jg='河北'
#查出人数最多的院系的男女生各多少人。
select sex, count(*) as 人数 from tab_stu where yxid = (select yxid from tab_stu group by yxid  order by count(*) desc limit 0,1) group by sex

#9)查出跟“河北女生”同院系的所有学生的信息。

select name from tab_stu where yxid=(select yxid from tab_stu where sex='女' and jg='河北')


.sql文件

/*
 Navicat Premium Data Transfer

 Source Server         : myconnct
 Source Server Type    : MySQL
 Source Server Version : 50617
 Source Host           : localhost:3306
 Source Schema         : db_0604

 Target Server Type    : MySQL
 Target Server Version : 50617
 File Encoding         : 65001

 Date: 04/06/2018 15:15:13
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for tab_stu
-- ----------------------------
DROP TABLE IF EXISTS `tab_stu`;
CREATE TABLE `tab_stu`  (
  `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `sex` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `jg` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `yxid` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `yxid`(`yxid`) USING BTREE,
  CONSTRAINT `tab_stu_ibfk_1` FOREIGN KEY (`yxid`) REFERENCES `tab_yxmes` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 7 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Records of tab_stu
-- ----------------------------
INSERT INTO `tab_stu` VALUES (1, '罗第华', '男', '江西', 1);
INSERT INTO `tab_stu` VALUES (2, '韩顺平', '男', '四川', 2);
INSERT INTO `tab_stu` VALUES (3, '吴英累', '男', '黑龙江', 1);
INSERT INTO `tab_stu` VALUES (4, '王玉虹', '女', '河北', 3);
INSERT INTO `tab_stu` VALUES (5, '赵玉川', '男', '河北', 3);
INSERT INTO `tab_stu` VALUES (6, '刘招英', '女', '江西', 1);

-- ----------------------------
-- Table structure for tab_yxmes
-- ----------------------------
DROP TABLE IF EXISTS `tab_yxmes`;
CREATE TABLE `tab_yxmes`  (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `yxmc` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '院系名称',
  `xbdz` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '系办地址',
  `xbdh` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '系办电话',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Records of tab_yxmes
-- ----------------------------
INSERT INTO `tab_yxmes` VALUES (1, '计算机系', '行政楼302', '01066886688');
INSERT INTO `tab_yxmes` VALUES (2, '数学系', '科研楼108', '010802820080');
INSERT INTO `tab_yxmes` VALUES (3, '物理系', '行政楼305', '0106768677');

SET FOREIGN_KEY_CHECKS = 1;


猜你喜欢

转载自blog.csdn.net/liu709127859/article/details/80567739