带有最新日期的SQL脚本

时间:2013-05-29 08:50:17

标签: php

我正在开发发票视图系统,我想在iframe中查看单个PDF发票。我不知道如何制作查看具有最新截止日期的单一发票的SQL脚本。

我使用的是Zend Framework,数据库是MySQL。

    public function viewinvoiceemployeeAction()
{
    /** Object variable. Example use: $logger->err("Some error"); */
    $logger = Zend_Registry::get('LOGGER');
    /** Object variable. Example use: $something = $config->database; */
    $config = Zend_Registry::get('config');
    /** Object variable. Example use: print $date->get(); */
    $date = Zend_Registry::get('date');
    /** Object variable. Example use: $stmt = $db->query($sql); */
    $db = Zend_Registry::get('dbAdapter');

    $request = $this->getRequest();

          $id = (integer) $request->getParam('ostoreskontra_id');
          $dateColumn = (string) date("Y-m-d", strtotime("NOW"));
          //select * from yourTable where dateColumn = (select max(dateColumn) from yourTable)

          $db->beginTransaction();

          try {

              $date = (string) date("Y-m-d", strtotime($db->fetchone("SELECT laskun_pvm FROM ostoreskontra WHERE laskunera_pvm = (SELECT MAX(".$db->quote($dateColumn, 'STRING').") FROM ostoreskontra);")));
              $number = (integer) $db->fetchone("SELECT laskun_nro FROM ostoreskontra WHERE laskunera_pvm = (SELECT MAX(".$db->quote($dateColumn, 'STRING').") FROM ostoreskontra);");
              $sublier = (string) str_replace(" ","+",$db->fetchone("SELECT toimittaja.nimi FROM ostoreskontra LEFT JOIN toimittaja ON ostoreskontra.toimittaja_id=toimittaja.toimittaja_id WHERE ostoreskontra.laskunera_pvm = (SELECT MAX(".$db->quote($id, 'STRING').") FROM ostoreskontra);"));

              $file = (string) $date.'_'.$sublier.'_'.$number.'.pdf';

              $db->commit();

              $content = file_get_contents(APPLICATION_PATH."/uploads/ostolaskut/".$file);

              if ($id==0) {

                  header('Content-Type: text/plain');

              } else {

                  header('Content-Type: application/pdf');
                  header("Content-Length: " . strlen($content) );
                  //header('Content-Disposition: attachment; filename='.$file);

                  echo $content;

              }

          } catch (Exception $e) {

          $db->rollBack();
          //$success = array('success' => false, 'msg' => $e->getMessage());
          echo $e->getMessage();

          }

          //print_r($name);

}

1 个答案:

答案 0 :(得分:0)

您没有向我们提供太多信息,但是...使用此信息按日期获取表格中的最后一条记录:

select * from yourTable where dateColumn = (select max(dateColumn) from yourTable)