• 【14】where字句


    1.简介

    -> where紧跟在from后执行,用来初选
    -> 语法
    where 条件
    -> 注意
    -> where子句对数据源直接进行筛选
    -> 筛选可以是表达式、判断、匹配、范围和空

    2.筛选条件为表达式

    将from子句中的数据源字段直接运算

    例:

    一张记录期中成绩与期末成绩的表,需要查询出考试及格的人,最终分数期中成绩占30%,期末成绩占70%,最终分数大于60分算及格。

    -- 创建表
    CREATE TABLE ScoreTbl (
    scoreId INT IDENTITY ( 1, 1 ) NOT NULL PRIMARY KEY,
    stuId INT NOT NULL,
    scoreNum INT CHECK ( scoreNum >= 0 AND scoreNum <= 100 ),
    scoreLast INT CHECK ( scoreLast >= 0 AND scoreLast <= 100 ),
    
    );
    -- 插入数据
    INSERT INTO ScoreTbl ( stuId, scoreNum, scoreLast )
    VALUES
        ( 1, 60, 55 ),
        ( 2, 75, 40 ),
        ( 3, 95, 85 ),
        ( 5, 86, 75 ),
        ( 6, 90, 95 );
    INSERT INTO ScoreTbl ( stuId, scoreNum, scoreLast )
    VALUES
        ( 7, 45, 99 );
    -- 查询及格的人
    SELECT
        *,
        scoreNum *.3+ scoreLast *.7 
    FROM
        ScoreTbl 
    WHERE
    scoreNum *.3+ scoreLast *.7>= 60;

    3.判断

    -> 使用=、<>、>、>=、<、<=进行判断

    -> 多个条件使用and和or连接

    -> 否定使用not

    -> 优先级为

    not > and > or

    例:查出总成绩及格,但是期中没有及格的学生

    SELECT
        *
        ,
        scoreNum *.3 + scoreLast *.7 
    FROM
        ScoreTbl 
    WHERE
        scoreNum *.3 + scoreLast *.7 >= 60 
        AND scoreNum < 60;

    4.模糊匹配

    -> 有时需要查询姓”牛”的人

    -> 语法

    使用”_”表示一个任意字符

    使用”%”表示任意个任意字符

    判断使用like,而非=

    例:找出名字叫亮亮的人,找出名字中包含虎的人

    -- 叫纪明X
    SELECT
        * 
    FROM
        TestDataBase..Student 
    WHERE
        stuName LIKE '纪明%';
    INSERT INTO TestDataBase..Student ( stuName, stuSex, stuBirthdate, stuStudydate, stuAddress, stuEmail, stuPhone, classId )
    VALUES
        ( '纪明闪闪', 'm', '1990-1-1 00:00:00', '2014-7-7 17:04:52.123', N'上帝细节128号', 'jmss@jmss.com', '12345678909', 2 );
    -----------------------
    -- [] [a-z] [^a-z]
    -- stuName like '杨[中重]科'
    -- 如果要匹配 asp_net
    -- bookName like 'asp[_]net'
    -- stuName like '%虎%'

    5.范围

    -> 判断一个字段的取值范围

    例:

    查询年龄是19到26岁的女生

    查询年龄是19、21、25和26岁的女生

    -> 可以使用between 左值 and 右值

    -> 可以使用in(散列可选值)

    -- age 在 19 到 26 岁
    -- datediff(year, 开始的时间, 结束的时间)
    SELECT
        datediff( YEAR, stuBirthdate, CURRENT_TIMESTAMP ),* 
    FROM
        TestDataBase..Student 
    WHERE
        stuSex = 'f' 
        AND -- datediff(YEAR, stuBirthdate, CURRENT_TIMESTAMP) between 19 and 26;
        datediff( YEAR, stuBirthdate, CURRENT_TIMESTAMP ) IN ( 19, 26, 23 );

    6.空值处理*

    -> 在表中的数据,有些是允许为空的,那么查询出性别为空的人怎么办呢?

    select * from Person where sex=null吗?

    -> 数据库中对于空的判断使用is

    select * from Person where sex is null

    -> 有时得到的数据中不希望有空,可以使用isnull函数

    -- 空值处理
    -------------------------SELECT
        * 
    FROM
        ConstraintExercise.StuInfo2 
    WHERE
        stuAge <> NULL;
    -- SQL Server 采用三值逻辑 真 假 不知道
    -- 判断为空使用 is null 或 is not null  再或 not(... is null)
    SELECT
        * 
    FROM
        ConstraintExercise.StuInfo2 
    WHERE
        stuAge IS NOT NULL;
    -- isnull(字段, 数据)
    SELECT
        *,
        isnull( stuAge, - 1 ) 
    FROM
        ConstraintExercise.StuInfo2;
  • 相关阅读:
    Spring----->projects----->Spring IO Platform(也即spring的BOM:Bill Of Materials)
    可添加功能-----机器学习管理平台
    Maven--->学习心得--->maven的Dependency Mechanism(依赖关系机制)
    Maven--->使用实例
    Maven--->学习心得--->maven的相关配置---->概述 以及 相关配置实例
    Maven--->学习心得--->maven的配置文件settings.xml
    netty学习记录(实例)
    netty学习记录
    打开CKeditor默认本地上传
    Spring Security 报There is no PasswordEncoder mapped for the id "null"
  • 原文地址:https://www.cnblogs.com/lolitagis02/p/8161526.html
Copyright © 2020-2023  润新知