同一条sql,php程序执行的结果和数据库执行的结果不一样,求解

WBOY
Libérer: 2016-06-23 13:59:06
original
1551 Les gens l'ont consulté

输出sql和结果集的代码如下

$sql=$sql1.$sql_initbest;echo $sql;//initRows4($start_date,$end_date,$objActSheet);//$months=getIntMonth($start_date,$end_date1);//$count_1=3*$months+3;//每个月有3列//echo $sql1.$sql_initbest;$query = mssql_query($sql);//$c=0;//$arr=Array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');//$count_4=0;//月份计数器//$n=4;//$arr1=array();while($row=mssql_fetch_array($query)){	print_r($row);	echo "<br><br>";
Copier après la connexion

输出结果 注意红框中的数据

把sql复制到数据库中执行结果如下

我确定是同一条sql,请问为什么会这样?


回复讨论(解决方案)

数据是在时时更新的吗?

1)贴出sql,拿到mysql编辑器执行看看。
2)在程序处输出sql,并且打印结果集看看。

1)贴出sql,拿到mysql编辑器执行看看。
2)在程序处输出sql,并且打印结果集看看。
都在帖子上有,用的是ms-sql数据库

select class2,January2014_Number_stores,January2014_Quantity_sold, January2014_qn=January2014_Quantity_sold/January2014_Number_stores,February2014_Number_stores,February2014_Quantity_sold, February2014_qn=February2014_Quantity_sold/February2014_Number_stores,March2014_Number_stores,March2014_Quantity_sold, March2014_qn=March2014_Quantity_sold/March2014_Number_stores from (select db_bi.class2,January2014_Number_stores = count(distinct case when left(db_bi.qsq,6) <='201401' then db_bi.dbno_bi else null end),January2014_Quantity_sold = SUM(case left(CMD..cmd_xjjx_sale.outdate,6) when '201401'then CMD..cmd_xjjx_sale.nb else 0 end),February2014_Number_stores = count(distinct case when left(db_bi.qsq,6) <='201402' then db_bi.dbno_bi else null end),February2014_Quantity_sold = SUM(case left(CMD..cmd_xjjx_sale.outdate,6) when '201402'then CMD..cmd_xjjx_sale.nb else 0 end),March2014_Number_stores = count(distinct case when left(db_bi.qsq,6) <='201403' then db_bi.dbno_bi else null end),March2014_Quantity_sold = SUM(case left(CMD..cmd_xjjx_sale.outdate,6) when '201403'then CMD..cmd_xjjx_sale.nb else 0 end),April2014_Number_stores = count(distinct case when left(db_bi.qsq,6) <='201404' then db_bi.dbno_bi else null end),April2014_Quantity_sold = SUM(case left(CMD..cmd_xjjx_sale.outdate,6) when '201404'then CMD..cmd_xjjx_sale.nb else 0 end) from db_bi ,CMD..cmd_xjjx_sale ,BI_Product_Information where db_bi.dbno = CMD..cmd_xjjx_sale.store and BI_Product_Information.colthno = CMD..cmd_xjjx_sale.colthno and CMD..cmd_xjjx_sale.outdate>='2014-01' and CMD..cmd_xjjx_sale.outdate<='2014-04' and charindex( '已撤',db_bi.names) = 0 and db_bi.class2 in ('A','B','C','O') and closeif <> 1 and flags_bi in('1','2','3','4')	 and CMD..cmd_xjjx_sale.endprice <> 0 group by db_bi.class2 ) a  order by class2
Copier après la connexion

class2 为 B、C 时的相应值都不一样
你能确定表数据没有变化吗?

数据是在时时更新的吗?
数据不是实时更新的

class2 为 B、C 时的相应值都不一样
你能确定表数据没有变化吗?
class2为B、C时值不一样;能确定表数据没变化

这下悲剧鸟....有好几个都是同样的sql用程序和直接在数据库编译器中执行的结果不一样( 确定是同样的sql,确定数据库不是实时更新的)
测试如下 代码:

<?phpinclude_once 'DB_connmssql.php';$sql="select class2 ,January2014_Number_stores,January2014_Quantity_sold,					January2014_qn=January2014_Quantity_sold/January2014_Number_stores,February2014_Number_stores,February2014_Quantity_sold,					February2014_qn=February2014_Quantity_sold/February2014_Number_stores,March2014_Number_stores,March2014_Quantity_sold,					March2014_qn=March2014_Quantity_sold/March2014_Number_stores from (select  db_bi.class2,January2014_Number_stores = count(distinct case  when left(db_bi.qsq,6)  <='201401' then db_bi.dbno_bi else null end),January2014_Quantity_sold = SUM(case left(CMD..cmd_xjjx_sale.outdate,6) when '201401'then CMD..cmd_xjjx_sale.nb else 0 end),February2014_Number_stores = count(distinct case  when left(db_bi.qsq,6)  <='201402' then db_bi.dbno_bi else null end),February2014_Quantity_sold = SUM(case left(CMD..cmd_xjjx_sale.outdate,6) when '201402'then CMD..cmd_xjjx_sale.nb else 0 end),March2014_Number_stores = count(distinct case  when left(db_bi.qsq,6)  <='201403' then db_bi.dbno_bi else null end),March2014_Quantity_sold = SUM(case left(CMD..cmd_xjjx_sale.outdate,6) when '201403'then CMD..cmd_xjjx_sale.nb else 0 end)from db_bi,CMD..cmd_xjjx_sale,BI_Product_Informationwhere db_bi.dbno = CMD..cmd_xjjx_sale.storeand BI_Product_Information.colthno = CMD..cmd_xjjx_sale.colthnoand CMD..cmd_xjjx_sale.outdate>='2014-01'and CMD..cmd_xjjx_sale.outdate<='2014-04'and charindex( '已撤',db_bi.names) = 0   and db_bi.class2  in ('A','B','C','O')  and closeif <> 1    and flags_bi in('1','2','3','4')	and CMD..cmd_xjjx_sale.endprice <> 0group by db_bi.class2) aorder by class2";$query = mssql_query($sql);while($row=mssql_fetch_array($query)){	print_r($row);	echo "<br><br>";}?>
Copier après la connexion

结果

那你检查一下是否为同一的数据库
不要 php 查询的是在用的库(数值大些),而控制台查询的是备份的库

那你检查一下是否为同一的数据库
不要 php 查询的是在用的库(数值大些),而控制台查询的是备份的库
确定是同一个数据库,而且发现了几个类似的问题,同样的sql用程序执行和直接在控制台执行的结果不一样。

那你检查一下是否为同一的数据库
不要 php 查询的是在用的库(数值大些),而控制台查询的是备份的库
找到原因了,经过对比发现sql中这一句

and charindex( '已撤',db_bi.names) = 0  
Copier après la connexion

这一个条件在php程序查询数据库时没起作用,换成 not like也不行,该怎么解决呢?

解决了把‘已撤’改为
". iconv('utf-8', 'gbk', '已撤')."

我看你里面有时间条件,是条件不一样吧

那你检查一下是否为同一的数据库
不要 php 查询的是在用的库(数值大些),而控制台查询的是备份的库
斑竹 请教另外个问题
在导出成excel文件时我用number_format函数对数据进行了处理,然后在最后求和是调用了excel的sum函数ru

$objActSheet->setCellValue($arr[$j].$n,"=SUM($x1:$x2)");
Copier après la connexion

导致求和的结果不对,如下。

请问怎么做才能保证数据格式和求和的结果正确?

不要用 number_format 函数格式化数据
而应该用单元格格式来控制,比如

$sheet->getStyle('D6')            ->getNumberFormat()            ->setFormatCode('#,##0');
Copier après la connexion

source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal