【问题标题】:SQL: cannot echo result when row is emptySQL:行为空时无法回显结果
【发布时间】:2012-08-23 16:36:48
【问题描述】:
$pdf->Table('select @N:=@N+1 AS no,
                    A.department as "DEPARTMENT", 
                    A.no as "ASSET NUMBER" , 
                    A.total as "ASSET TOTAL PRICE" , 
                    B.no as "INVENTORY NUMBER" ,  
                    B.total as "INVENTORY TOTAL PRICE" 
                from 
                    (select  h.department , coalesce(count(h.id),0) as no,coalesce(sum(h.price),0) as total 
                        from asset h 
                        where h.department = "'.$department.'" 
                        and  year(h.date_accepted) = '.$year.' 
                        GROUP BY year(h.date_accepted)) as  A , 
                    (select coalesce(count(i.id),0) as no,coalesce(sum(i.price),0) as total 
                        from inventory i 
                        where i.department = "'.$department.'"
                        and  year(i.date_accepted) = '.$year.' 
                        GROUP BY year(i.date_accepted)) as B   ',$prop);

我正在制作一个 PDF 文件,该文件将生成从 SQL 读取的表。 问题是当数据库中的任何行为空时,即使其他表中有数据,PDF 表也不会生成任何输出。

例如2009 年只接受资产,而没有库存。回显时,即使数据库的资产表中有数据,生成的表也是空的。

我尝试了从 ISNULL、IFNULL、COALESCE 到将 sum()/count() 的值设置为 0(我认为生成的表为空的原因)的所有方法,但仍然无法生成数据。

为什么 COALESCE 函数不起作用,是因为使用了别名吗?例如 COALESCE(sum(i.price),0))(如 i.variable)

【问题讨论】:

  • 不管这里发生了什么,看起来至少存在三个严重的 SQL 注入错误。我真的希望这些值被转义。

标签: mysql sql pdf-generation fpdf coalesce


【解决方案1】:

更改您的 PHP 代码而不是 db 表,使用 "" 或任何内容自行检查和设置数据,如果这些在 echo 之前为空。

【讨论】:

  • 如何设置 sum(i.price) 的值?因为它是变量的总和,而不是变量本身。为什么 COALESCE 功能不起作用?
猜你喜欢
  • 1970-01-01
  • 2019-06-30
  • 1970-01-01
  • 2014-06-09
  • 1970-01-01
  • 1970-01-01
  • 2013-08-04
  • 1970-01-01
  • 2017-07-17
相关资源
最近更新 更多