使Zend \ Db(zf2)包含在子句之间

时间:2013-01-08 18:05:12

标签: mysql zend-framework2 between

我目前正在使用Zend Framework 2和带有日期范围的查询来从MySQL数据库中获取数据,并且我遇到了先前在ZF1中不可用的between子句。

但是,我看起来像这样的代码无法正常工作:

   $dateStart = '2012-12-20';
   $dateEnd = '2012-12-31';

   $sql = new Sql($_db);
   $select = $sql->select()
                ->from(array("t" => $table))
                ->columns(array("col1" => "col_as_1", "col2" => "col_as_2"));

 $select->where->between("date", $dateStart, $dateEnd);
 $stmt = $sql->prepareStatementForSqlObject($select);
 $result = $stmt->execute()->getResource()->fetchAll(\PDO::FETCH_ASSOC);

显然,条款不具有包容性,我只能在2012-12-30之前得到结果,有没有办法让它具有包容性?我一直在看看ZF2文档,但它们不是很有帮助,在MySQL查询浏览器上运行相同的查询会返回我需要的所有数据。

5 个答案:

答案 0 :(得分:3)

$from_date = date('Y-m-d', strtotime($AppCleaner->from_date ));
$to_date = date('Y-m-d', strtotime($AppCleaner->to_date ));

  $select->where->between('appointment_date', $from_date . ' 00:00:00', $to_date . ' 23:59:59');

另外,使用下面的between子句:

        $sql = new Sql($this->adapter);
        $select = $sql->select();
        $select->from('app_posts');
        $select->where->NEST->between( 'id', 30,40);
        $select->group('app_posts.id');
 //     echo $select->getSqlString($this->adapter->getPlatform());
//      exit;
        $statement = $sql->prepareStatementForSqlObject($select);
        $result = $statement->execute();
        $resultSet = new ResultSet();
        $resultSet->initialize($result);
        $posts = $resultSet->buffer()->toArray();
        return $resultSet;

答案 1 :(得分:2)

因此,您可以尝试lessThanOrEqualTogreaterThanOrEqualTo

之间似乎没有提供此功能:between($identifier, $minValue, $maxValue)

如果使用$select->__toString()跟踪查询,则可以将查询视为字符串。

我的计算机上没有ZF2,但我可以想象ZF2中的between会输出date > '2012-12-20' AND date < '2012-12-31'

答案 2 :(得分:2)

请注意:在Mysql上使用Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim NewRow As New TableRow Dim NewCell As New TableCell Dim rblOptions As New RadioButtonList rblOptions.ID = "Option1" rblOptions.Items.Add(New System.Web.UI.WebControls.ListItem("1", "1")) rblOptions.Items.Add(New System.Web.UI.WebControls.ListItem("2", "2")) rblOptions.Items.Add(New System.Web.UI.WebControls.ListItem("3", "3")) NewCell.Controls.Add(rblOptions) NewRow.Cells.Add(NewCell) 'Questions is a table Questions.Rows.Add(NewRow) End Sub Protected Sub SaveButton_Click(sender As Object, e As EventArgs) Handles SaveButton.Click If Page.IsValid Then Dim rbl As RadioButtonList = DirectCast(Questions.FindControl("Option1"), RadioButtonList) If rbl.SelectedValue.ToString = "ExpectedValue" Then End If End If End Sub

between

注意:您应该将 +1 date 插入 endDate 。因为当你将2015-05-18日期插入 endDate 时。你无法获得2015-05-18的数据。所以你需要加一个日期结束日期

你可以用这个

来做
date_column_name between 'startDate' AND 'endDate'

答案 3 :(得分:1)

BETWEEN应该是包容性的,你确定日期之后没有小时,分钟和秒,这将导致它不会选择2012-12-31以来的日期2012-12-31 00:00:01会技术上是&gt; 2012-12-31

答案 4 :(得分:0)

格式必须相同,使用mysql DATE函数

$select->where->between("DATE(date)", DATE('.$dateStart.'), DATE('.$dateEnd.'));