目录
设定汉语别名时需要使用双引号(“”)括起来
查询常数:
- select
- '商品' as "string"
- ,38 as number
- ,'2009-02-24' as date
- from table
在SQL语句中使用字符串或者日期常数时,必须使用单引号(’‘)将其括起来
在select语句中使用distinct可以删除重复行,也就是以前理解的去重
- select
- distinct product_type
- ,regist_date
- from product
distinct 也可以在多列之前使用,会将多个列的数据进行组合,将重复的数据合并为一条;如上,对'product_type'列和'regist_date'列的数据进行组合,将重复的数据合并为1条;disticnt关键字只能用在第一个列名之前。
where 子句要紧跟在 from 子句之后
所有包含 null 的计算,结果都是 null
<> 不等于
字符串类型的数据原则上按照字典顺序进行排序,不能与数字的大小顺序混淆。
查询null时不能使用比较运算符(= 或者<>),要用is null 或者is not null。
not 不能单独使用,要和其他查询条件组合起来使用,比如:
- select
- a
- ,b
- ,c
- from table
- where not a >= 1000
- ## 与 where a < 1000 是等价的
not 运算符用来否定某一条件,但是不能滥用。
- select
- product_name
- ,product_type
- ,regist_date
- from product
- where product_type = '办公用品'
- and regist_date = '2009-09-11'
- or regist_date = '2009-09-20'
and运算符优先级高于or运算符,上面的sql会被翻译成,“商品种类为办公用品,并且登记日期为2009年9月11日” 或者是 “登记日期是2009年9月20日”,应该改成:
- select
- product_name
- ,product_type
- ,regist_date
- from product
- where product_type = '办公用品'
- and ( regist_date = '2009-09-11' or regist_date = '2009-09-20')
所以,and运算符优先级高于or运算符,想要优先执行or运算符时需要使用括号。
除了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