数据库SQL查询语句大全

阅读: 评论:0

2024年1月27日发(作者:)

数据库SQL查询语句大全

distinct关键字

显示没有重复记录的商品名称,商品价格和商品类别列表。

select distinct ware_name,price from t_ware;

使用计算列

查询所有商品价格提高20%后的价格。

select ware_id,ware_name,price*1.2 from t_ware;

列的别名

a) 不使用as

select ware_id,ware_name,price*1.2 as price_raise from t_ware;

b) 使用as

select ware_id,ware_name,price*1.2 price_raise from t_ware;

使用逻辑表达式

a) not

显示商品价格不大于100的商品

select ware_id,ware_name,price,category_id from t_ware where not price>100;

b) and

显示商品价格大于100且商品类别编号为5的商品

select ware_id,ware_name,price,category_id from t_ware where not price>100;

c) 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;

使用between关键字

显示商品价格在200元至1000元之间的商品(留心一下,是半开区间还是封闭区间?)

select ware_id,ware_name,price,category_id from t_ware where price between 200 and

1000;

使用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;

使用like子句进行模糊查询

a) %(百分号)表示0到n个任意字符

select ware_id,ware_name,price,category_id from t_ware where ware_name like '%纯棉%';

b) _(下划线)表示单个的任意字符

select ware_id,ware_name,price,category_id from t_ware where ware_name like '%长袖_恤%';

转义字符escape的使用

select ware_id,ware_name,price,category_id from t_ware where ware_name like '%%%'

escape '';

使用order by给数据排序

select * from t_ware_category where parent_id=0 order by seq;

select * from t_ware_category where parent_id=0 order by seq asc;

select * from t_ware_category where parent_id=0 order by seq desc;

rownum

a) 查询前20条商品记录

select ware_id,ware_name,price 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);

常用统计函数

a) sum()返回一个数字列或计算列的总和

select sum(price) from t_ware;

b) avg()对一个数字列或计算列求平均值

c) min()返回一个数字列或一个数字表达式的最小值

d) max()返回一个数字列或一个数字表达式的最大值

e) count()返回满面足select语句中指定的条件的记录值

多表查询和笛卡儿乘积

查询商品编号,商品名称,商品价格和商品类别名称

select t__id,t__name,t_,t_ware_ry_name

t_ware,t_ware_category where t_ry_id=t_ware_ry_id;

使用join

a) 左连接

select t__id,t__name,t_,t_ware_ry_name

t_ware left join t_ware_category on t_ry_id=t_ware_ry_id;

select _id,_name,,ry_name from t_ware w left

from

from

join

t_ware_category wc on ry_id=ry_id;

b) 右连接

select t__id,t__name,t_,t_ware_ry_name

t_ware left join t_ware_category on t_ry_id=t_ware_ry_id;

from

使用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 '%手提包%'

使用group by

a) 统计每个二级类别下有多少商品,以及商品总价值

select ry_id,ry_name,count(_id),sum() from t_ware w

left join t_ware_category wc on ry_id=ry_id group by

ry_id,ry_name;

b) 统计每个一级类别下有多少商品,以及商品总价值

select ry_id,ry_name,sum() from t_ware w left join t_ware_category

wc on ry_id=ry_id left join t_ware_category wc2 on

_id=ry_id group by ry_id,ry_name;

使用having对结果进行筛选

select ry_id,ry_name,count(_id),sum() from t_ware w left join

t_ware_category wc on ry_id=ry_id group by ry_id,ry_name

having sum()>1000;

数据库SQL查询语句大全

本文发布于:2024-01-27 03:48:22,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/1706298502992.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:商品   类别   使用   数字   商品价格   查询   语句   显示
留言与评论(共有 0 条评论)
   
验证码:
排行榜

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23