zoukankan      html  css  js  c++  java
  • mysql 语句

    周日闲着无事,想起半年前有个面试sql语句的,说我答的不怎么样,特此研究一下。
    首先建立学生,学科,成绩表,花时间自己建的。
    SET FOREIGN_KEY_CHECKS=0;
    -- ----------------------------
    -- Table structure for `course`
    -- ----------------------------
    DROP TABLE IF EXISTS `course`;
    CREATE TABLE `course` (
      `Cno` tinyint(2) NOT NULL COMMENT '程课号',
      `Cname` varchar(125) NOT NULL COMMENT '课程名',
      `Cpon` tinyint(2) NOT NULL COMMENT '先行课',
      `Ccredit` tinyint(2) NOT NULL COMMENT '学分',
      PRIMARY KEY (`Cno`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
     
    -- ----------------------------
    -- Records of course
    -- ----------------------------
    INSERT INTO `Course` VALUES ('1', '数据库', '5', '4');
    INSERT INTO `Course` VALUES ('2', '数学', '3', '2');
    INSERT INTO `Course` VALUES ('3', '信息系统', '1', '4');
    INSERT INTO `Course` VALUES ('4', '操作系统', '6', '3');
    INSERT INTO `Course` VALUES ('5', '数据结构', '6', '3');
    INSERT INTO `Course` VALUES ('6', '数据处理', '0', '2');
    INSERT INTO `Course` VALUES ('7', 'PASCAL 语言', '6', '4');
     
    -- ----------------------------
    -- Table structure for `sc`
    -- ----------------------------
    DROP TABLE IF EXISTS `sc`;
    CREATE TABLE `sc` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `Sno` int(11) NOT NULL,
      `Cno` int(11) NOT NULL,
      `Grade` tinyint(2) NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
     
    -- ----------------------------
    -- Records of sc
    -- ----------------------------
    INSERT INTO `SC` VALUES ('1', '200215121', '1', '92');
    INSERT INTO `SC` VALUES ('2', '200215121', '2', '85');
    INSERT INTO `SC` VALUES ('3', '200215121', '3', '88');
    INSERT INTO `SC` VALUES ('4', '200215122', '2', '90');
    INSERT INTO `SC` VALUES ('5', '200215122', '3', '80');
     
    -- ----------------------------
    -- Table structure for `student`
    -- ----------------------------
    DROP TABLE IF EXISTS `student`;
    CREATE TABLE `student` (
      `Son` int(11) NOT NULL,
      `Sname` char(25) NOT NULL,
      `Ssex` char(15) NOT NULL,
      `Sage` tinyint(6) NOT NULL,
      `Sdep` char(6) NOT NULL,
      PRIMARY KEY (`Son`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
     
    -- ----------------------------
    -- Records of student
    -- ----------------------------
    INSERT INTO `Student` VALUES ('200215121', '李勇', '男', '20', 'CS');
    INSERT INTO `Student` VALUES ('200215122', '刘晨', '女', '19', 'CS');
    INSERT INTO `Student` VALUES ('200215123', '王敏', '女', '18', 'MA');
    INSERT INTO `Student` VALUES ('200215125', '张离', '男', '19', 'IS');
     
     
    1,查询经过计算的值,
    SELECT Sname,2004-Sage FROM student;
    嗯,数字类型字段可直接进行加减乘除的运算,字符串可直接用mysql函数改变其值。
     
    2,用distinct取消重复,
    SELECT DISTINCT sno FROM sc
     
    3,查询满足条件的,
    这里就不详细讲了,
    =, >, < ,<=,>=,!或<>,!>,!<。
    between and 和 not betwween and
    in()和not in
    like %表示任意长度字符串,_表示单个字符串。
    IS NULL 或 IS NOT NULL 注意这里写 = 号无效。
    order by 对结果排序的。
     
    查询比较多,这里分小点吧,
    1)  聚合查询,
    一共就几个聚合函数,count
    SELECT COUNT(*) as c,COUNT(Ccredit) as b ,COUNT(DISTINCT Ccredit) as c ,COUNT(Cpon) as a FROM course;
    嗯,这里就很有意思了,首先count(*) 速度是最快的,然后count某个字段会排出空行,加上distinct 会排出重复,另外as重名的话,mysql会自动给第二个重名的后面加上1.
     
    注意两点,给聚合函数查出的结果排序的话,用group by, 限定条件的话用having。
     
    2)链接查询,
    left join ,right join ...on a.id=b.id。这懒得说了。注意笛卡尔积现象。
     
    子查询注意子句在= > 号等符号后面,还可以在in,all,any等谓词的后面。当然all,any的前面还是要有符号的。
    select Sname,
    Sage from student 
    where sage<any 
    (select sage from student 
    where Sdep='CS') 
    and Sdep <>'CS';
    还有一个谓词常用到,exists。
    select sname
    from student
    where exists
    (select * from SC where
     sno=student.son and cno='1');
    嗯,建表时貌似名字敲颠倒了。
    一般掌握这几个关键字可应付绝大多数子查询了。
  • 相关阅读:
    【转】Oracle 建立索引及SQL优化
    SQL Server Express LocalDB 存入中文产生乱码问题
    安装(c)npm及搭建Vue项目详解
    【转】VSCode 调试 Node.js 介绍
    【转】C#根据用户信息,生成token和cookie的方法
    远程连接 Docker 的 MySQL 服务
    mysql服务器和服务器启动程序
    一台物理机上运行多个mysql实例(Running Multiple MySQL Instances on One Machine)
    mysql5.7主从切换(master/slave switchover)
    选项文件(Option Files)/配置文件(Configuration Files)的使用
  • 原文地址:https://www.cnblogs.com/guanliyang/p/3635433.html
Copyright © 2011-2022 走看看