简体   繁体   English

Zend Framework DB_TABLE中的mySQL语句

[英]mySQL statement in Zend Framework DB_TABLE

I'd like to know how to properly write mySQL statement using ZEND in this case: 在这种情况下,我想知道如何使用ZEND正确编写mySQL语句:

SELECT * FROM `users` WHERE role_id = 2 AND status = 2 AND bddate BETWEEN '1988-05-07' AND '1991-01-01' ORDER BY lastname DESC

I'll have if statements, for ex. 我要有if语句,例如。 if in the filter user selected role_id I'll include it in WHERE and so on, the same with the rest of them. 如果在过滤器用户选择的role_id中,我将其包含在WHERE中,依此类推,其余部分相同。

So far I have 1 WHERE condition and I have done it like this (now I need more complicated one): 到目前为止,我有1个WHERE条件,并且我已经这样完成了(现在我需要一个更复杂的条件):

    $select = $this->select()
            ->setIntegrityCheck(false)
            ->from(array('u'=>'users'))
            ->join(array('r'=>'user_roles'), 'u.role_id = r.role_id', array('role'=>'title'));          
    if(count($filters) > 0) {
        foreach($filters as $field => $filter) {
            $select->where($field . ' = ?', $filter);
        }
    }

    if(null != $sortField) {

        $select->order($sortField);

    }

Okay, so you are having a problem with the BETWEEN. 好的,所以您在BETWEEN方面遇到问题。 There are two possible solutions for this: 有两种可能的解决方案:

a) You use two where clauses for to split up the BETWEEN, so you get a)您使用两个where子句拆分BETWEEN,因此您得到

$select = $this->select()
//... other conditions
    ->where('bddate >= ?', '1988-05-07')
    ->where('bddate <= ?', '1991-01-01');

OR 要么

b) b)

$select = $this->select()
//... other conditions
    ->where ('bddate BETWEEN '1988-05-07' AND '1991-01-01');

I would recommend option a), because it properly escapes your values. 我建议选择选项a),因为它可以正确地逸出您的价值观。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM