天天看点

17、oracle:max,in,or,trunc,sql变量的使用以及分号

1.max

 (1)首先注意max等聚合类函数只能作用于数据组,其将from后面的整个表格记录作为一个数据组,对此数据组的单列进行最大值选取,因此select中不能和其他字段一起用,如果非要一起,必须使用group by,例如:

selectmax(a),b from x group by b;---按照b分组,每组找出最大的a

selectmax(a),b,c from x group by b,c;---找出b,c在相同的情况下,a的最大值(待测试)

selectmax(a),max(b),c from x group by c;---找出c相同时的a的最大值和b的最大值(待测试)

而如下则为错误

SELECT TNAME, DNAME, TSEX, MAX (AGE)  FROM TEACHER ---错误

   SELECT TNAME, DNAME, TSEX,SAL ,AGEFROMTEACHER WHERE AGE=MAX (AGE) ---错误

只能使用子查询的方式来实现

SELECT TNAME, DNAME, TSEX, SAL, AGE

FROM    TEACHER

WHERE  AGE=(SELECT MAX (AGE) FROM    TEACHER)

  (2)max用于int型数字的最大值选取很容易想到,但是字符串和日期类型数据就不一定能想到了

  Select max(tname) from teacher;

  Select * fromteacher where tbegintime in (select max(begintime) from teacher)—选出teacher表中入职时间最大(迟)的完整记录

说明:对字符型数据的最大值,是按照首字母由A~Z的顺序排列,越往后,其值越大。当然,对于汉字则是按照其全拼拼音排列的,若首字符相同,则比较下一个字符,以此类推。

说明:确定列中的最大值(最小值)时,MAX( )(MIN( ))函数忽略NULL值。但是,如果在该列中,所有行的值都是NULL,则MAX( )/MIN( )函数将返回NULL值。

此处参考文章:http://www.cnblogs.com/ymj126/p/3912827.html

2.in

  (1)一般in是为了减少or条件的重复使用

SELECT*

FROMorders

WHEREorder_id in (10000, 10001, 10003, 10005);

  等于

SELECT*

FROMorders

WHEREorder_id = 10000

ORorder_id = 10001

ORorder_id = 10003

ORorder_id = 10005;

  还可以是字符串,日期类型等。

  (2)一般使用max的时候获取的是一个字段的值,此时一般想要获取max的结果集字段在全表中的完整记录(就是选出所有包含有max结果集字段的所有记录),会使用到in

  Select * fromteacher where tbegintime in (select max(begintime) from teacher where…)—选出teacher表中满足一定条件的入职时间最大(迟)的完整记录

3.or

 如果逻辑上需要多个or和and条件连用,注意要使用括号,比如.....where  id = 25 and (name = "aaa" or age = 25)

Select* from record where id = 38 and trunc(endtime) = trunc(sysdate) or trunc(begintime)= trunc(sysdate))---错误,无法处理这种语法

可以改为

Select* from record where id = 38 and ( trunc(endtime) = trunc(sysdate) or trunc(begintime)= trunc(sysdate))

或改为

select* from  record

where  endtime in (select endtime  from  record

  where id = 38 and trunc(endtime) =trunc(sysdate))

or   begintime in (select begintime from record

  where id = 38 and trunc(begintime) =trunc(sysdate))

4.trunc:类似截取函数,按指定的格式截取输入的数据。

 (1)处理日期

语法格式:TRUNC(date[,fmt])

  其中:date 一个日期值;fmt 日期格式。

    该日期将按指定的日期格式截取;忽略它则由最近的日期截取

示例:

   select trunc(sysdate) from dual;--2017/2/13,返回当前时间(无格式控制,去最近日期)

   select trunc(sysdate,'yy') fromdual;--2017/1/1,返回当年第一天

   select trunc(sysdate,'mm') fromdual;--2017/2/1,返回当月的第一天

   select trunc(sysdate,'d') fromdual;--2017/2/12,返回当前星期的第一天,即星期天

   selecttrunc(sysdate,'dd') from dual;--2017/2/13,返回当前日期,今天是2017/2/13

   select trunc(sysdate ,'HH24') fromdual;--2017/2/13 15:00:00,返回本小时的开始时间

   select trunc(sysdate ,'MI') fromdual;--2017/2/13 15:13:00,返回本分钟的开始时间

(2)处理number型数字

语法格式:TRUNC(number[,decimals])

其中: number 待做截取处理的数值;decimals 指明需保留小数点后面的位数,可选项,忽略它则截去有的小数部分。

注意:截取时并不对数据进行四舍五入

示例:

    select trunc(123.567,2) from dual;--123.56,将小数点右边指定位数后面的截去;

    select trunc(123.567,-2) from dual;--100,第二个参数可以为负数,表示将小数点左边指定位数后面的部分截去,即均以0记;(这里,-2理解为将小数点左边第二位及后面所有位全部用0代替)

select trunc(123.567) from dual;--123,默认截去小数点后面的部分;

这部分参考文章:http://www.cnblogs.com/linjiao/p/6394087.html

5.c#中的sql语句变量和分号

(1)string selectSql = @" select ID,name from student where ID = {0} and name = ‘{1}’ " ;

stringSql = string.Format(selectSql, myid, myname);

使用占位符来表示sql中的变量,注意双引号里面是不可以有分号的

(2)String [email protected]"select ID,name from studentwhere name= ' "+aaa+ " ' ";//字符串型

[email protected]"select ID,name from student where ID="+aaa;//int型

(3):+变量名

注意,如果变量为日期类型,则必须使用“:变量名”的用法,否则无法通过编译,报出各种奇怪的异常

string selectSql = @" select recordid, customerid, productid  from  a

                             where begintime=:OpTime or endtime=:OpTime ";

 OracleParameter argOpTime = new OracleParameter(":OpTime", OracleDbType.Date);

argOpTime.Value = realOpTime;

其中OracleParameter为封装好的类,OracleDbType为枚举类型

继续阅读