trzxycx 发表于 2018-10-24 06:18:48

SQL查询语句关键字方法

  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.2price_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
页: [1]
查看完整版本: SQL查询语句关键字方法