- sql不区分大小写
- 别名用as
1.查询select
检索单个列
select prod_name
from products;
(多条sql用;号分离,单条可以不加,最好加上)
查询多个列
select prod_id,prod_name,prod_price
from products;
查询所有的列-用通配符*
select *
from products;
查询不同的值-distinct返回不同的值
select distinct vend_id
from products;
注:distinct作用于所有的列,不仅仅是其后面那列。
限制返回结果条数-limit,offset从哪开始
只检索前5行数据:
select prod_name
from products
limit 5
检索从5行起的10行数据:
select prod_name
from products
limit 10 offset 5
2.排序order by
- 应该保证order by是select 字句中最后一条字句
先按价格排,价格一致按名称排
select prod_id,prod_price,prod_name
from products
order by prod_price,prod_name
降序desc
产品降序排序
select prod_id,prod_price,prod_name
from products
order by prod_price desc,prod_name # desc只应用到直接前面的列名
3.过滤where
=等于
<> !=不等于
<小于
大于
!> 不大于
between 在两个值之间
3.1过滤单个值
select prod_name,prod_price
from products
where prod_price <10
select prod_name,prod_price
from products
where prod_price != 'xxx'
3.2and 、in、or
- 有and 和or 先处理and
- 可以加括号提高优先级,圆括号的优先级高于and和or
# and 满足所有条件
select prod_id,prod_price,prod_name
from products
where vend_id = 'DLL01' and prod_price <=4
# or 满足任意条件
select prod_id,prod_price,prod_name
from products
where vend_id = 'DLL01' or prod_price <=4
# in 加圆括号
select prod_name,prod_price
from products
where vend_id in ('DLL01','D002')
order by prod_name
3.3not
select prod_name
from products
where not vend_id = 'DLL01'
order by prod_name
3.4通配符
like
% 表示任意字符出现任意次
_只匹配单个字符,而不是多个字符
[]指定一个字符集
# 找出以Fish开头的产品 'Fish%'
select prod_id,prod_name
from products
where prod_name like 'Fish%'
# 找出任何位置在有bean bag的
select prod_id,prod_name
from products
where prod_name like '%bean bag%'
# 找出以F开头、以y结尾的
select prod_name
from products
where prod_name like 'F%y'
# _正好只匹配一个字符,__正好匹配两个字符
select prod_id,prod_name
from products
where prod_name like '__inch teddy bear'
## 找出所有名字J或M开头的联系人
select cust_contact
from customers
where cust_contact like '[JM]%'
order by cust_contact
3.5创建计算字段*
# 拼接字段,拼接同一个表的两列
select concat(vend_name,'(',vend_coountry,')')
from vendors
order by vend_name
补充 常用函数
rtrim 去掉列值右边的空格
upper 文本转换大写
4.汇总数据 avg count max min sum
avg() 返回某列的平均值
count() 返回某列的行数
max() 返回某列的最大值
min() 返回某列的最小值
sum() 返回某列值之和
avg
avg()函数忽略列值为null的行
select avg(prod_price) as avg_price
from products
where vend_id = 'DLL01'
count
count(*)对表中行的数目进行计数
count(列名)对特定的列具体的行进行计数
select count(cust_email) as num_cust
from customers
max
select max(prod_price) as max_price
from products
min
select min(prod_price) as min_price
from products
sum
select sum(quantity) as items_ordered
from orderitems
where order_num = 20005
加distinct 聚集不同的值
select avg(distinct prod_price) as avg_price
from products
where vend_id = 'DLL01'
注:
distinct只能用于count(列名),不能用于 min()和max()
5.分组 group by 、having
创建分组group by
select vend_id,count(*) as num_prods
from products
group by vend_id
注意:
-
group by 必须在where字句之后,order字句之前
-
group by不能使用别名
-
group by 每一列都必须是检索列或有效表达式(但不能是聚集函数)
-
如果是在select中使用表达式,则必须在group by字句中指定相同的表达式
having 过滤分组**
group和where的区别?
where过滤行,group by 过滤分组
where是数据分组前过滤,having数据分组后过滤
select cust_id,count(*) as orders
from orders
group by cust_id
having count(*) >= 2 # 两个以上订单
group by、having、where
where 过滤所有prod_price至少为4的行,然后按vend_id分组数据,having 字句过滤计数为2或2以上的分组
select vend_id ,count(*) as num_prods
from products
where prod_price >=4
group by vend_id
having count(*) >= 2
分组group by 和排序 order by
select order_num,count(*) as items
from orderitems
group by order_num
having count(*) >=3
order by items,order_num
select子句顺序
select
from
where
group by
having
order by
6.子查询
子查询:嵌套在其他查询中的查询
子查询常用于where 子句的in 操作符中,以及用来填充计算列。
利用子查询进行过滤
假设
orders表只存客户id,订单编号,订单日期
顾客的实际信息存储在customers表中
ordersitems存储各个订单的物品
# 假设需要查询订购RGA01的所有顾客?
# 1.检索包含物品RGAN01的所有订单编号,得到20007,20008的订单编号
select order_num
from orderitems
where prod_id = 'RGSN01'
# 2.检索具有前一步所有订单编号的所有顾客ID,得到顾客id(1000000007,100000008)
select cust_id
from orders
where order_num IN (20007,20008)
# 3.检索前一步返回的 顾客ID的所有顾客信息,
select cust_name,cust_contact
from customers
where cust_id (1000000007,100000008)
# 合成一条语句
select cust_name,cust_contact
from customers
where cust_id in (select cust_id
from orders
where orders_num in (select order_num
from orderitems
where prod_id = 'RGAN01'))
作为计算字段使用
select cust_name,
cust_state,
(select count(*)
from orders
where orders.cust_id = customers.cust_id ) as orders
from customers
order by cust_name
7.联结join
主键:具有唯一标识
内联结
# 写法一
select vend_name,prod_name,prod_price
from venders,products
where venders.vend_id = products.vend_id
# 写法一
select vend_name ,prod_name,prod_price
from vendors
inner join products on vendors.vend_id = products.vend_id
外联结(包含没有关联行的那些行)
select customers.cust_id,orders.order_num
from custmoers
left outer join
orders on customers.cust_id = ordres.cust_id
在使用outer join语法时,必须使用right或left 关键字指定包括其所有行的表。
其他特性
主键
-
主键是一种特殊的约束,用来保证一列中的值是唯一的
-
任意两行的主键值都不相同
-
每行都具有一个主键值,不允许为null
-
包含主键值得列从不修改或更新
-
主键不能重用,如果从表中删除某一行,其主键不分配给新行
外键
外键是表中的一列,其值必须列在另一表中的主键中
以上内容来自SQL必知必会第5版整理