如何比较当前结果集的值与前一个?

时间:2010-10-25 13:22:58

标签: php mysql

请查看代码段

while ($rslt = fetch_array($result))
   {

     //$row++;
     if (bccomp ($row, $row_limit, 0) != 1)
       {
         /*HEADER*/            

         if ($row == 0)
           {
             foreach ($_SESSION['market']['data'] as $title => $field)
               {
                 //Output Header Cell

                 //$title++;
                 $worksheet->write ($row, $col, title,$formatHeader);                     
                   //Advance Column
                 $col++;
                 }
               }

             //Reset Column Position
             $col = 0;

             //Advance Row
              $row++;
           }

         /*BODY*/
         foreach ($_SESSION['market']['data'] as $title => $field)
           {


             //Output Body Cell
             $worksheet->write ($row, $col, $rslt[$field], $formatNormal);

             //Advance Column
             $col++;
           }

         //Reset Column Position
         $col = 0;

         //Advance Row
         $row++;
       }

   }

它的工作完全正常,我很满意。但现在要求是:在每一行的第0列有一个名称,并且就该名称还有其他字段,现在我想做的是比较每一行的第0列,问题是如何存储前一行的第0列。逻辑就像($ rslt ['name'] === $ rslt [// previous column name])。 请把我解雇。

1 个答案:

答案 0 :(得分:2)

您无法使用state别名。您必须重复IFNULL()表达式:

... CONCAT(a, aa, IFNULL((SELECT p.name FROM p WHERE p.id = x.name), 'N.A.'))

此外,我认为您的意思是CONCAT()而不是CONCATE()。另请注意,N.A.应包含在单引号中:'N.A.'


测试用例:

SELECT CONCAT('1-', '2-', IFNULL((SELECT NULL), 'N.A.'));
+---------------------------------------------------+
| CONCAT('1-', '2-', IFNULL((SELECT NULL), 'N.A.')) |
+---------------------------------------------------+
| 1-2-N.A.                                          |
+---------------------------------------------------+
1 row in set (0.00 sec)

SELECT CONCAT('1-', '2-', IFNULL((SELECT 3), 'N.A.'));
+------------------------------------------------+
| CONCAT('1-', '2-', IFNULL((SELECT 3), 'N.A.')) |
+------------------------------------------------+
| 1-2-3                                          |
+------------------------------------------------+
1 row in set (0.00 sec)