SQL基础(二) 查询

笔记整理自《SQL基础教程(日)》第2章 http://www.ituring.com.cn/book/1086

2-1) select语句基础

#基本语法
select <列名1>, <列名2>,...
from <表名>;

 

1. 查询表中列下对应的数据

#查询出表Product中的3列
select product_id, product_name, purchase_price
from Product;
#查看整个表 (按create table语句定义时对列的排序)
select *
from Product;

注:SQL使用换行符或半角空格来分隔单词。

2. 为列设定别名

#执行结果中,表头全都换成别名,而非create table语句时设定的名称
select product_id as id, 
product_name as name, 
purchase_price as price
from Product;
#设定中文别名,用双引号,不能用单引号
select product_id as "商品编号“,
product_name as “商品名称“,
purchase_price as “进货单价”
from Product;

3. 常数的查询

#将字符串常数,数字常数,日期常数查询出来
select '商品' as string, 38 as number, '2018-09-20' as date, product_id, product_name
from Product;

4. 从结果中删除重复行

#删除product_type列中的重复数据
select distinct product_type
from Product;

Null也被视为一类数据. 

#多列检索重复数据,只有当2行数据,每列对应的记录都相同,才会被合并
select distinct product_type, regist_date
from Product;

5. 根据where语句来选择记录

#select语句中where子句的语法
select <列名1>, <列名2>,...
from <表名>
where <条件表达式>;
#选取product_type列为'衣服'的记录
select product_name, product_type
from Product
where product_type = '衣服';
#注意,where子句必须紧跟在from子句之后
select product_name
from Product
where product_type = '衣服';

6. SQL中的注释

# 1行注释用 -- 即可
-- 本select语句会从结果中删除重复行
select distinct product_id, purchase_price
from Product;

# 多行注释 /* 和 */之间
/* 本select语句,
会从结果中删除重复行 */
select distinct product_id, purchase_price
from Product;

 

2-2) 算术运算符和比较运算符

1. 运算是以行为单位执行的。()括号中的运算表达式优先级提升。

select product_name, sale_price,
sale_price * 2 as "sale_price_*2"
from Product;

2. 带有 Null 的运算

--下面运算在SQL中的结果,会是什么呢?
5 + NULL
10 - NULL
1 * NULL
4 / NULL
NULL / 9
NULL / 0

答案全部是Null.  因为在SQL中,所有包含NULL的计算,结果还是NULL.

3. 比较运算符

-- 等于符号 =
select product_name, product_type
from Product
where sale_price = 500;
-- 不等于符号 <>
select product_name, product_type
from Product
where sale_price <> 500;
-- 大于等于符号 >=
-- 大于符号 >
-- 小于等于符号 <=
-- 小于符号 <
select product_name, product_type, sale_price
from Product
where sale_price >= 100;

select product_name, product_type, regist_date
from Product
where regist_date < '2018-09-27';
select product_name, sale_price, purchase_price
from product
where sale_price - purchase-price >= 500;

4. 对字符串使用不等号

-- DDL: 创建表
create table Chars
(chr char(3) not null,
primary key (chr));

-- DML: 插入数据
start transaction;
insert into chars values ('1');
insert into chars values ('2');
insert into chars values ('3');
insert into chars values ('10');
insert into chars values ('11');
insert into chars values ('222');
commit;
-- 选取大于'2'的数据
select chr
from Chars
where chr > '2';

char列为‘字符串类型’,对该类型数据进行“大小比较”时,使用的是和“数字”不同的规则.

规则是,按照条目在字典中出现的顺序进行排序。因此,以相同字符开头的单词,比不同字符开头的单词更相近.

因此,它们的排列顺序为:'1', '10', '11', '2', '222', '3'

5. 不能对 NULL 使用比较运算符

-- 执行以下2条代码,对比输出的结果
select product_name, purchase_price
from Product
where purchase_price = 2800;

-- select product_name, purchase_price
from Product
where purchase_price <> 2800;
-- IS NULL 运算符 (选取NULL记录)
select product_name, purchase_price
from Product
where purchase_price is null;

-- IS NOT NULL (选取不为NULL的记录)
select product_name, purchase_price
from Product
where purchase_price is not null;

 

2-3) 逻辑运算符

1. NOT 运算符

-- 选出销售单价 >= 1000的记录
select product_name, product_type, sale_price
from Product
where sale_price >= 1000;

-- 以上代码添加 NOT 运算符, 获得 < 1000的记录
select product_name, product_type, sale_price
from Product
where not sale_price >= 1000;

2. AND & OR 运算符

-- and运算符,取交集
select product_name, purchase_price
from Product
where product_type = '厨房用具'
and sale_price >= 3000;
-- or运算符,取并集
select product_name, purchase_price
from Product
where product_type = '厨房用具‘
or sale_price >= 3000;

3. 通过括号强化处理

“商品种类为办公用品” 并且 “登记日期是2018年10月9号” 或者是 “2018年10月1号”

-- 注意以下代码运算符的顺序,and 优于 or 运算符
select product_name, product_type, regist_date
from Product
where product_type = '办公用品'
and regist_date = '2018-10-09' 
or regist_date = '2018-10-01'

-- 通过括号,让 or 运算符优于 and 执行
select product_name, product_type, regist_date
from Product
where product_type = '办公用品'
and ( regist_date = '2018-10-09'
or regist_date = '2018-10-01');

4. 逻辑运算符和真值

真值包括 TRUE 和 FALSE

SQL中有 “UNKNOWN”这种值,即除了“真” “假”之外的第3种值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值