数据库简单的查询语句

selsct
  由一系列子语句组成,这些子语句允许你从单个表或者多个表中选取单行或者多行数据。
Select子句和from是不可缺省的,必须包含在每个查询语句中。
  Selsct 子句 :指定需要查询的数据项,这些数据项可能来自表中的列名或者有sql在执行查询时计算出的表达式。
  From :指明数据的来源
  Where :列出搜索条件。
  group by: 用于对查询结果分组,将在某些列上具有相同取值的行为当作一组,没一组仅产生一个查询结果,group by是select语句的重要补充。
  Having 与group by配合使用,用于限制分组的条件,,即只有使用having中指定的条件满足分组才能出现在查询结果中。
  Order by:用于将查询结果按一列或者多列中的数据排序输出。省略order by的结果是查询无序。
  Into temp :将查询结果保存到一张临时表中。
Eg:select manu-code,description from stock where init-price〉400 order by unit-price。
Distinct,unique 用于去除重复的行
Eg:select distinct customer-num from oders
  Eg:select phone[1,3] from customer
  Eg:select ‘(’||phone[1,3]||’)’||phone[5,12],company from customer

Where 子语句:
==, 〉,!=,〉=,〈 ,〈=
空值判断 is eg:select order-num,customer-num from orders where paid-date is null
范围判断 between and ,eg: select order-num from orders where customer between 100 and 108
组属性判断:eg: select *from custmer where customer-num in(101,105)
模式匹配: eg:select *from customer where company=‘city‘

Like       matches            含义
%            *          可以与任意长度的字符匹配
_            ?           可以与任意单个字符匹配
\           \            转义符
           []             指定某个字符的取值范围
Eg: select * from manufact where manu-name matches ‘[a-n]*’//查找含有a到n的
Eg: select *from manufact where manu-name matches ‘[an]*’//查找含有a或者n的
复合条件查询
Or ,and,not
Eg:select * from stock where description=‘golf shoed’ or (description=‘rennis’and not (manu=‘anz’))
计算列
Eg :select stock-num,description,unit-price*1.05 from stock where manu-code=‘anz’
  //输出结果中与计算列对于的列用 “(expression)”表示。
Eg:select stock-num,description,unit-price*1.05 new-price from stock where manu-code=‘anz’//计算列变成了new-price
Eg: select company,‘site-in’ from customer where state =‘ca’//计算列只包含常数
Order by子句
  Asc 升序 ,desc 降序
多列排序
Eg: select *from stock order by manu-code,unit-price desc
     select *from stock order by manu-code desc,unit-price
     select *from stock order by unit-price, manu-code desc
     select *from stock order by unit-price desc, manu-code
列序号:一个列的列序号指的是这个列名在子句中的位置,最左边为1.
Eg:select customer-num,order-num,po-num,order-date from orders where paid-data is not null order by 4,1 =
select customer-num,order-num,po-num,order-date from orders where paid-data is not null order by order-date,customer-num
保存查询结果:
  Eg:select * from customer where state=‘ca’order by  company into temp cust-in-ca

猜你喜欢

转载自wuwy.iteye.com/blog/1569009