= '$datestart' AND t.statistics_date <= '$dateend'";
if($ver)
{
$condition .= " AND t.edition='$ver'";
}
if($coopid)
{
$condition .= " AND t.suco_coopid=$coopid";
}
if($subcoopid)
{
$condition .= " AND t.suco_subcoopid=$subcoopid";
}
if($sortfield && $sorttype){
$condition .= " ORDER BY t.{$sortfield} {$sorttype} NULLS LAST";
}elseif($sortfield){
$condition .= " ORDER BY t.{$sortfield} desc NULLS LAST";
}else{
$condition .= " ORDER BY t.statistics_date desc NULLS LAST";
}
$finish = $start + $rowsperpage;
$joinsqlcollection = "(SELECT tc.coop_name,tsc.suco_name,tsc.suco_coopid,tsc.suco_subcoopid,s.edition,s.new_user,d.one_user,d.three_user,d.seven_user,s.statistics_date FROM (((pdt_stat_newuser_{$pid}_{$plat} s LEFT JOIN pdt_days_dl_remain_{$pid}_{$plat} d ON s.statistics_date=d.new_date AND s.subcoopid=d.subcoopid AND s.edition=d.edition )LEFT JOIN tbl_subcooperator@JTUSER1.NET@JTINFO tsc ON s.subcoopid=tsc.suco_subcoopid) LEFT JOIN tbl_cooperator@JTUSER1.NET@JTINFO tc ON tsc.suco_coopid=tc.coop_id))";
$sql = "SELECT * FROM (SELECT tb_A.*,ROWNUM AS rn FROM (SELECT t.* FROM $joinsqlcollection t {$condition} ) tb_A WHERE ROWNUM <= {$finish} ) tb_B WHERE tb_B.rn>{$start} ";
$countsql = "SELECT COUNT(*) AS totalrows,SUM(t.new_user) AS totalnewusr,SUM(t.one_user) AS totaloneusr,SUM(t.three_user) AS totalthreeusr,SUM(t.seven_user) AS totalsevenusr FROM $joinsqlcollection t {$condition} ";
$db = oralceinit(1);
$stidquery = $db->query($sql,false);
$output = array();
while($row = $db->FetchArray($stidquery,$skip = 0,$maxrows = -1))
{
$output['data'][] = array_change_key_case($row,CASE_LOWER);
}
$count_stidquery = $db->query($countsql,false);
$row = $db->FetchArray($count_stidquery,$maxrows = -1);
$output['total']= array_change_key_case($row,CASE_LOWER);
//echo "
".($sql)."
";
return $output;
}
}
Null与空字符串' '的区别
含义解释:
问:什么是NULL?
答:在我们不知道具体有什么数据的时候,也即未知,可以用NULL,我们称它为空,ORACLE中,含有空值的表列长度为零。
ORACLE允许任何一种数据类型的字段为空,除了以下两种情况:
1、主键字段(primary key),
2、定义时已经加了NOT NULL限制条件的字段
说明:
1、等价于没有任何值、是未知数。
2、NULL与0、空字符串、空格都不同。
3、对空值做加、减、乘、除等运算操作,结果仍为空。
4、NULL的处理使用NVL函数。
5、比较时使用关键字用“is null”和“is not null”。
6、空值不能被索引,所以查询时有些符合条件的数据可能查不出来,count(*)中,用nvl(列名,0)处理后再查。
7、排序时比其他数据都大(索引默认是降序排列,小→大),所以NULL值总是排在最后。
使用方法:
select 1 from dual where null=null;
没有查到记录
select 1 from dual where null='';
没有查到记录
select 1 from dual where ''='';
没有查到记录
select 1 from dual where null is null;
1
---------
1
SQL> select 1 from dual where nvl(null,0)=nvl(null,0);
1
1
对空值做加、减、乘、除等运算操作,结果仍为空。
select 1+null from dual;
SQL> select 1-null from dual;
SQL> select 1*null from dual;
SQL> select 1/null from dual;
查询到一个记录.
注:这个记录就是SQL语句中的那个null
设置某些列为空值
现有一个商品销售表sale,表结构为:
共输入12条记录
select * from sale where sell like '%';
MONTH SELL
------ ---------
200001 1000
200002 1100
200003 1200
200004 1300
200005 1400
200006 1500
200007 1600
200101 1100
200202 1200
200301 1300
200008 1000
查询到11记录.
结果说明:
查询结果说明此SQL语句查询不出列值为NULL的字段
此时需对字段为NULL的情况另外处理。
select * from sale where sell like '%' or sell is null;
SQL> select * from sale where nvl(sell,0) like '%';
MONTH SELL
------ ---------
200001 1000
200002 1100
200003 1200
200004 1300
200005 1400
200006 1500
200007 1600
200101 1100
200202 1200
200301 1300
200008 1000
200009
查询到12记录.
Oracle的空值就是这么的用法,我们最好熟悉它的约定,以防查出的结果不正确。
但对于char 和varchar2类型的数据库字段中的null和空字符串是否有区别呢?
作一个测试:
insert into test(a,b) values('1','1');
SQL> insert into test(a,b) values('2','2');
SQL> insert into test(a,b) values('3','');--按照上面的解释,b字段有值的
SQL> insert into test(a) values('4');
SQL> select * from test;
A B
---------- ----------
1 1
2 2
3
4
select * from test where b='';
----按照上面的解释,应该有一条记录,但实际上没有记录
未选定行
select * from test where b is null;
----按照上面的解释,应该有一跳记录,但实际上有两条记录。
update table test set b='' where a='2';
SQL> select * from test where b='';
未选定行
select * from test where b is null;
A B
---------- ----------
2
3
4
测试结果说明,对char和varchar2字段来说,''就是null;但对于where 条件后的'' 不是null。
对于缺省值,也是一样的!
(编辑:4S站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!