关于SQL查询语句关键字方法
[var]
1、distinct关键字
显示没有重复记录的商品名称,商品价格和商品类别列表
select distinct ware_name,price from t_ware;
2、使用计算列
查询所有商品价格提高20%后的价格
select ware_id,ware_name,price*1.2 from t_ware’
3、列的别名
a) 不使用as
select ware_id,ware_name,price*1.2 price_raise
from t_ware;
from t_ware;
b)使用as
select ware_id,ware_name,price*1.2 price_raise
from t_ware;
from t_ware;
4、使用逻辑表达式
a)not 显示商品价格不大于100的商品
select ware_id,ware_name,price,category_id
from t_ware
where not price>100;
from t_ware
where not price>100;
b)or 显示商品类别编号为5或6或7的商品
select ware_id,ware_name,price,category_id
from t_ware
where category_id=5 or category_id=6
or category_id=7;
from t_ware
where category_id=5 or category_id=6
or category_id=7;
c)and 显示商品价格大于100且商品类别编号为5的商品
select ware_id,ware_name,price,category_id
from t_ware
where not price>100 and category_id = 5;
from t_ware
where not price>100 and category_id = 5;
5、使用between关键字
显示商品价格在200元至1000元之间的商品(留心一下,是半开区间还是封闭区间?)
select ware_id,ware_name,price,category_id
from t_ware
where price between 200 and 1000;
from t_ware
where price between 200 and 1000;
6、使用in关键字
显示商品类别为5,6,7且价格不小于200元的商品
select ware_id,ware_name,price,category_id
from t_ware
where category_id in (5,6,7) and price>=200;
from t_ware
where category_id in (5,6,7) and price>=200;
7、使用like子句进行模糊查询
a)%(百分号)表示0到n个任意字符
select ware_id,ware_name,price,category_id
from t_ware
where ware_name like ‘%纯棉%’;
from t_ware
where ware_name like ‘%纯棉%’;
b)_(下划线)表示单个的任意字符
select ware_id,ware_name,price,category_id
from t_ware
where ware_name like ‘%长袖_恤%’;
from t_ware
where ware_name like ‘%长袖_恤%’;
8、转义字符escape的使用
select ware_id,ware_name,price,category_id
from t_ware
where ware_name like ‘%\%%’ escape ‘\’;
from t_ware
where ware_name like ‘%\%%’ escape ‘\’;
9、使用order by给数据排序
select * from t_ware_category
where parent_id = 0 order by ware_id ;
——–
select * from t_ware_category
where parent_id = 0 order by ware_id asc;
———
select * from t_ware_category
where parent_id = 0 order by ware_id desc ;
where parent_id = 0 order by ware_id ;
——–
select * from t_ware_category
where parent_id = 0 order by ware_id asc;
———
select * from t_ware_category
where parent_id = 0 order by ware_id desc ;
rownum
a)查询前20条商品记录
select ware_id,ware_name,price
from t_ware
where rownum <= 20;
from t_ware
where rownum <= 20;
b)查询第11条至第20条记录
select ware_id,ware_name,price
from t_ware
where rownum<=10 and ware_id
not in(select ware_id from t_ware where rownum<=10);
from t_ware
where rownum<=10 and ware_id
not in(select ware_id from t_ware where rownum<=10);
10、常用统计函数
a) sum()返回一个数字列或计算列的总和
select sum(price) from t_ware;
b) avg()对一个数字列或计算列球平均值
c) min()返回一个数字列或一个数字表达式的最小值
d) max()返回一个数字列或一个数字表达式的最大值
e) count()返回满足select语句中指定的条件的记录值
11、多表查询和笛卡尔乘积
查询商品编号,商品名称,商品价格和商品类别名称
select
t_ware.ware_id, t_ware.ware_name, t_ware.price ,t_ware_category_name
from t_ware, t_ware_category
where t_ware.category_id=t_ware_category.category_id;
t_ware.ware_id, t_ware.ware_name, t_ware.price ,t_ware_category_name
from t_ware, t_ware_category
where t_ware.category_id=t_ware_category.category_id;
使用join
a)左连接
select
t_ware.ware_id,t_ware.ware_name,t_ware.price,t_ware_category.category_name
from t_ware
left join t_ware_category
on t_ware.category_id=t_ware_category.category_id;
t_ware.ware_id,t_ware.ware_name,t_ware.price,t_ware_category.category_name
from t_ware
left join t_ware_category
on t_ware.category_id=t_ware_category.category_id;
select w.ware_id,w.ware_name,w.price,wc.category_name
from t_ware w
left join t_ware_category wc
on w.category_id=wc.category_id;
from t_ware w
left join t_ware_category wc
on w.category_id=wc.category_id;
b) 右连接
select t_ware.ware_id,t_ware.ware_name,t_ware.price,t_ware_category.category_name
from t_ware
left join t_ware_category
on t_ware.category_id=t_ware_category.category_id;
from t_ware
left join t_ware_category
on t_ware.category_id=t_ware_category.category_id;
12、使用union
select ware_id,ware_name
from t_ware
where ware_name like ‘%T恤%’
union
select ware_id,ware_name
from t_ware
where ware_name like ‘%手提包%’
from t_ware
where ware_name like ‘%T恤%’
union
select ware_id,ware_name
from t_ware
where ware_name like ‘%手提包%’
13、使用group by
a)统计每个二级类别下有多少商品,以及商品总价值
select w.category_id,wc.category_name,
count(w.ware_id),sum(w.price)
from t_ware w
left join t_ware_category wc
on w.category_id=wc.category_id
group by w.category_id,wc.category_name;
count(w.ware_id),sum(w.price)
from t_ware w
left join t_ware_category wc
on w.category_id=wc.category_id
group by w.category_id,wc.category_name;
b) 统计每个一级类别下有多少商品,以及商品总价值
select wc2.category_id,wc2.category_name,sum(w.price)
from t_ware w
left join t_ware_category wc
on w.category_id=wc.category_id
left join t_ware_category wc2
on wc.parent_id=wc2.category_id
group by wc2.category_id,wc2.category_name;
from t_ware w
left join t_ware_category wc
on w.category_id=wc.category_id
left join t_ware_category wc2
on wc.parent_id=wc2.category_id
group by wc2.category_id,wc2.category_name;
14、使用having对结果进行筛选
–举例子说明:查询table表查询每一个班级中年龄大于20,性别为男的人数
select COUNT(*)as ‘>20岁人数’,classid
from Table1
where sex=’男’
group by classid,age having age>20
from Table1
where sex=’男’
group by classid,age having age>20
需要注意说明:当同时含有where子句、group by 子句 、having子句及聚集函数时,执行顺序如下:
执行where子句查找符合条件的数据;
使用group by 子句对数据进行分组;对group by 子句形成的组运行聚集函数计算每一组的值;最后用having 子句去掉不符合条件的组。
- having 子句中的每一个元素也必须出现在select列表中。有些数据库例外,如oracle.
- having子句和where子句都可以用来设定限制条件以使查询结果满足一定的条件限制。
- having子句限制的是组,而不是行。where子句中不能使用聚集函数,而having子句中可以。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。