• 【SQL 初级语法 1】 查询基础


    目录

    1 查询基础

    1.1 select 语句基础

    1、列的查询

    2、删除重复行

    3、where

    1.2 算术运算符和比较运算符

    1、需要注意 null

    2、比较运算符

    1. 3 逻辑运算符

    1、not 运算符

    2、通过括号强化处理

    3、逻辑运算符和真值


    1 查询基础

    1.1 select 语句基础

    1、列的查询

    设定汉语别名时需要使用双引号(“”)括起来

    查询常数:

    1. select
    2. '商品' as "string"
    3. ,38 as number
    4. ,'2009-02-24' as date
    5. from table

    在SQL语句中使用字符串或者日期常数时,必须使用单引号(’‘)将其括起来

    2、删除重复行

    在select语句中使用distinct可以删除重复行,也就是以前理解的去重

    1. select
    2. distinct product_type
    3. ,regist_date
    4. from product

    distinct 也可以在多列之前使用,会将多个列的数据进行组合,将重复的数据合并为一条;如上,对'product_type'列和'regist_date'列的数据进行组合,将重复的数据合并为1条;disticnt关键字只能用在第一个列名之前。

    3、where

    where 子句要紧跟在 from 子句之后

    1.2 算术运算符和比较运算符

    1、需要注意 null

    所有包含 null 的计算,结果都是 null

    2、比较运算符

    <> 不等于

    字符串类型的数据原则上按照字典顺序进行排序,不能与数字的大小顺序混淆。

    查询null时不能使用比较运算符(= 或者<>),要用is null 或者is not null。

    1. 3 逻辑运算符

    1、not 运算符

    not 不能单独使用,要和其他查询条件组合起来使用,比如:

    1. select
    2. a
    3. ,b
    4. ,c
    5. from table
    6. where not a >= 1000
    7. ## 与 where a < 1000 是等价的

    not 运算符用来否定某一条件,但是不能滥用。

    2、通过括号强化处理

    1. select
    2. product_name
    3. ,product_type
    4. ,regist_date
    5. from product
    6. where product_type = '办公用品'
    7. and regist_date = '2009-09-11'
    8. or regist_date = '2009-09-20'

    and运算符优先级高于or运算符,上面的sql会被翻译成,“商品种类为办公用品,并且登记日期为2009年9月11日” 或者是 “登记日期是2009年9月20日”,应该改成:

    1. select
    2. product_name
    3. ,product_type
    4. ,regist_date
    5. from product
    6. where product_type = '办公用品'
    7. and ( regist_date = '2009-09-11' or regist_date = '2009-09-20')

    所以,and运算符优先级高于or运算符,想要优先执行or运算符时需要使用括号。

    3、逻辑运算符和真值

    除了true 和false意外还有第三种Boolean值——不确定(unknown)其他语言通常都是二值逻辑,只有SQL中的逻辑运算称为三值逻辑。

    虽然是三值逻辑,但是真值表依然只使用true和false展示2*2,4行,如果考虑添加null的话就会变成3*3=9行,繁琐而且条件判断也变得异常复杂,所以大家达成了“尽量不使用null”的共识。

    ————————————————————————————————————————

     点击链接 查看SQL 专栏更多文章:https://blog.csdn.net/weixin_46249441/category_11913899.html?spm=1001.2014.3001.5482

  • 相关阅读:
    SpringBootCMS漏洞复现分析
    十七、redux
    薅!语雀致歉送6个月会员;万字教程讲透AI视频生成;提示词14个黄金设计法则;吴恩达AI职业规划指南 | ShowMeAI日报
    TreeMap和HashMap的区别
    抛弃chatgpt,使用微软的Cursor提升coding效率
    若依分页问题
    购物车的html
    神经网络简介
    计划评审技术
    php继承(extends)
  • 原文地址:https://blog.csdn.net/weixin_46249441/article/details/125759162