zoukankan      html  css  js  c++  java
  • dljd_(011-020)_简单的查询

    一、不等号查询条件

      1.1查询学生表(student)中年龄(age)不等于20的所有学生信息   

            select * from student where age!=20;
    
            select * from student where age<>20;

       1.2查询学生表(student)中生日(brthday)不是1998-10-01日的所有学生的信息  

            select * from student where birthday !='1998-10-01';
        
            select * from student where birthday <>'1998-10-01';

      1.3查询学生表(student)中职位()不是班长的所有同学的信息  

            select * from student where isMonitor !='班长';
            select * from student where isMonitor <>'班长';

    二、<、<=、>、>=条件查询

      2.1查询学生表(student)中成绩(score)小于80的所有学生的信息  

            select * from student where score<80;

      2.2查询学生表(student)中成绩(score)小于等于80的所有学生的信息

            select * from student where score<=80;

      大于(>)、大于等于(>=)的用法同小于(<)、小于等于(<=)

    三、between...and的使用

      注意:between...and作用于数值型和日期时间型上是包头包尾、而作用于字符型上是包头不包尾!

      3.1查询学生表(student)中年龄(stuage)在23(包含)到25(包含)之间的所有学生信息

          select * from student where stuage between 23 and 25;

      3.2查询学生表(student)中出生日期(stubirthday)在'1994-01-01'(包含)到‘1996-12-31’(包含)之间的所有学生信息

            select * from student where stubirthday between '1994-01-01' and '1996-12-31';

      3.3查询学生表(student)中学生姓名(stuname)在a到f之间的所有学生信息  

        select * from student where stuname between 'a' and 'f';

       这里需要注意的是between ...and...作用于字符型上是包头,不包尾、也就是以上sql查询结果中不会出现以f开头的学生名。如果要出现那么将‘f’改为下个字母即‘g’。

    四、and查询条件的用法

      sql中用and连接两个条件的时候、查询结果必须同时都满足这两个条件。and可以使用&&代替

      4.1查询学生表(student)中年龄(stuage)小于24、并且成绩(stuscore)大于80的同学信息 

            select * from student where (stuage<24) and (stuscore>80);
            select * from student where (stuage<24) && (stuscore>80);

     五、or查询条件的用法

      sql中使用or连接两个条件的时候,查询结果只需要满足其中的一个条件即可。and可以使用||代替

      5.1查询学生表(student)中年龄(stuage)小于24、或者成绩(stuscore)大于80的同学信息  

            select * from student where (stuage<24) or (stuscore>80);
            select * from student where (stuage<24) || (stuscore>80);

     六、and和or同时存在条件中时的优先级问题

      and和or同时存在条件中时:and的优先级要比or高。所以我们用括号来将or条件提升。

      6.1查询学生(student)表中成绩(stuscore)大于80、并且年龄(stuage)是24或这是26的学生的信息  

            --错误写法
            select * from student where stuscore>80 and stuage=24 or stuage=26;
            --正确写法
            select * from student where stuscore>80 and (stuage=24 or stuage=26);    

    七、in/not in在查询条件中的用法

      7.1查询学生表(student)中年龄(stuage)是22或24的学生的信息  

            --用in
            select * from student where stuage in(22,24);
            --用or
            select * from student where stuage=22 or stuage=24;
        

      7.2查询学生表(student)中年龄(stuage)不是22或24的学生的信息  

            --用not in
            select * from student where stuage not in(22,24);
            --用and
            select * from student where stuage<>22 and stuage!=24;    

     八、模糊查询like

      %表示匹配0个或多个任意字符。

      _表示一个任意的字符

      8.1查询学生表(student)中名字(stuname)中包含a的学生的信息 

            select * from student where stuname like "%a%";

      8.2查询学生表(student)中名字(stuname)以a开头的学生的信息

            select * from student where stuname like "a%";   

      8.3查询学生表(student)表中学生的名字(stuname)第二个字符是"a"的学生信息  

         select * from student where stuname like "_a%";

      8.4查询学生表(student)中学生名字(stuname)中倒数第三个字符是a的学生信息  

            select * from student where stuname like "%a__";

       8.5查询学生表(student)中学生名字(stuname)中含有%的学生的学生信息(这里就需要使用转移符)

            select * from student where stuname like "%%%";

      

      

    如有任何疑问可联系邮箱: 给我发邮件、或直接联系QQ:1584875179 || 点返回首页

  • 相关阅读:
    mvc使用model进行数据的增加修改的方法
    c#导出word在iis部署上报异常
    做个转圈圈的咚咚
    VS2008中AJAX的部署问题(工具箱中无AJAX Extensions选项卡)
    关于 AjaxControlToolkit requires ASP.NET Ajax 4.0 scripts. 错误
    ASP.NET关于继承DropDownList的自定义DDL控件
    线性表顺序表示的C#实现(参考数据结构(C语言版))
    WORD2003出现的乱码
    线性表链式表示的C#实现(参考数据结构(C语言版))
    有错误先找自己的原因(若你百度不出为什么vista开网页慢,可以来试试这方法)
  • 原文地址:https://www.cnblogs.com/aeon/p/10109052.html
Copyright © 2011-2022 走看看