zoukankan      html  css  js  c++  java
  • SQL语句练习

    都是在网上找的别人整理好的,为了自己能够方便找。感谢作者!

    1. http://www.cnblogs.com/GT_Andy/archive/2009/12/25/1921911.html

    2.http://blog.csdn.net/wenboliang/article/details/4095859

    如下题目都是上面的链接中的题目,为了方便自己操作就稍作修改。如下主要记录了自己在使用sql语句的过程中犯错的地方。

    1.用一条sql语句,查出每门课都在10分以上的学生。表格名称为C1
    n+------+---------+-------+
    | name | subject | grade |
    +------+---------+-------+
    | 1    | A       |    11 |
    | 1    | B       |    10 |
    | 2    | A       |    11 |
    | 2    | B       |    12 |
    | 3    | A       |     9 |
    | 3    | B       |    12 |
    | 4    | A       |    12 |
    | 4    | B       |    15 |
    | 4    | C       |    16 |
    | 4    | C       |    16 |
    +------+---------+-------+

    分析:该表没有主键。
    思路1:
    选出分数在10分以下的学生姓名作为a,选出不在a里的所有学生的姓名。
    语句1:
    select name from T1 and name not in (select name from T1 where fenshu < '80') as a;
    验证:此语句错误。
    此处不能用and连接。
    and与where的区别:
    where 用来表达一个筛选条件,而and用来连接两个或者两个以上的筛选条件。
    语句2:
    select name from T1 where name not in (select name from T1 where fenshu < '80') as a;
    验证:此语句错误。
    未去除重复项。sql中文本才需要用单引号,数值不用单引号。not in/in 后的选择语句不需要使用别名。

    修正语句:
    select distinct name from C1 where name not in (select name from C1 where grade < 10);
    其他方法:


    2.一个叫 team 的表,里面只有一个字段name, 一共有4 条纪录,分别是a,b,c,d, 对应四个球对,现在四个球对进行比赛,用一条sql 语句显示所有可能的比赛组合。
    ------+
    | name |
    +------+
    |    1 |
    |    2 |
    |    4 |
    |    3 |

    思路: 从两张表中选。
    语句1:
    select C2.name as name1 , C3.name as name2 from C2 , (select * from C2) as C3 where C2.name <> C3.name;
    验证:
    13与31等重复。这样的结果是排列的结果,而不是组合!
    答案:
    select C2.name as name1 , C3.name as name2 from C2 , (select * from C2) as C3 where C2.name < C3.name;
    其他方法:






  • 相关阅读:
    微服务架构技术栈选型手册(万字长文)
    Visual Studio 2013 always switches source control plugin to Git and disconnect TFS
    Visual Studio 2013 always switches source control plugin to Git and disconnect TFS
    MFC对话框中使用CHtmlEditCtrl
    ATL开发 ActiveX控件的 inf文件模板
    ActiveX: 如何用.inf和.ocx文件生成cab文件
    Xslt 1.0中使用Array
    如何分隔两个base64字符串?
    An attempt was made to load a program with an incorrect format
    JQuery 公网 CDN
  • 原文地址:https://www.cnblogs.com/simone-wenwen/p/4135046.html
Copyright © 2011-2022 走看看