Zend框架sql查询产生不同的结果

时间:2011-02-17 07:38:26

标签: php sql mysql zend-framework

我正在尝试在zend中执行此sql查询:

SELECT a.id AS aid, 
    a.assignment_name AS name, 
    IFNULL((a.id = sa.aid),'0') AS status,
    a.assignment_name AS assignment_name,
        c.class_name AS class_name,
    a.due_date AS due_date,
        a.post_date AS post_date,
       (a.id = sa.aid) AS status,
       IF(a.description="",'No description given',a.description) AS description,
       IF(a.grading="",'No grading policy',a.grading) AS policy,
       CONCAT(u.prefix,' ',u.lname) AS teacher
FROM `assignments` as a
JOIN classes AS c ON a.class_id = c.id
JOIN enrollments as e ON e.student_id = 9 AND e.approved = 1 AND c.id = e.class_id
JOIN users as u on u.id = a.teacher_id
LEFT JOIN student_assignments as sa ON a.id = sa.aid
group by a.id;

我的zend代码是这样的:

public function getAllStudent($count, $offset) {
        $result = Zend_Db_Table::getDefaultAdapter()->select()
                        ->from(array('a' => 'assignments'),
                                array(
                                    'id' => 'a.id',
                                    'assignment_name' => 'a.assignment_name',
                                    'class_name' => 'c.class_name',
                                    'due_date' => 'a.due_date',
                                    'post_date' => 'a.post_date',
                                    'status' => "(a.id = sa.aid)",
                                    'description' => 'IF(a.description="",\'No description given\',a.description)',
                                    'policy' => 'IF(a.grading="",\'No grading policy\',a.grading)',
                                    'teacher' => 'CONCAT(u.prefix,\' \',u.lname)'
                        ))

                        ->join(array('c' => 'classes'), 'a.class_id = c.id', array())
                        ->join(array('e' => 'enrollments'), 'e.student_id = ' . $this->auth->id . ' AND e.approved = 1 AND c.id = e.class_id', array())
                        ->join(array('u' => 'users'), 'u.id = a.teacher_id', array())
                        ->joinLeft(array('sa' => 'student_assignments'), 'a.id = sa.id', array())
                        ->group('a.id')
                        ->order('a.due_date DESC')
                        ->limit($count, $offset)
                        ->query()
                        ->fetchAll();
        return $result;
    }

我从我的控制器中调用它如下:

$this->view->assignments = $this->assignments->getAllStudent(20, 0);

当我在navicat(第一段代码段落)中执行此查询时,我得到以下结果:

ID         name  !STATUS!   assignment_name  class   date              etc                      etc
24  Kill Animal 0   Kill Animal biology 2011-01-27  2011-01-26      You must kill a frog    Instant death = 100points. Slow death = 40points    Mr. Mihai
25  Solve Exercies  1   Solve Exercies  Math    2011-01-31  2011-01-26  1   You must solve all exercises    All exercises = 100points   Mr. Mihai

一切都很好,这是我想要的结果。看看STATUS专栏(我已经把它强调了)。一个结果STATUS = 0,另一个STATUS = 1.这是应该返回的结果。然而 如果我使用上面的代码(第二个代码段落)执行相同的查询,我得到以下内容:

array(2) {
  [0]=>
  array(9) {
    ["id"]=>
    string(2) "25"
    ["assignment_name"]=>
    string(14) "Solve Exercies"
    ["class_name"]=>
    string(4) "Math"
    ["due_date"]=>
    string(10) "2011-01-31"
    ["post_date"]=>
    string(10) "2011-01-26"
    ["status"]=>
    NULL
    ["description"]=>
    string(28) "You must solve all exercises"
    ["policy"]=>
    string(25) "All exercises = 100points"
    ["teacher"]=>
    string(9) "Mr. Mihai"
  }
  [1]=>
  array(9) {
    ["id"]=>
    string(2) "24"
    ["assignment_name"]=>
    string(11) "Kill Animal"
    ["class_name"]=>
    string(7) "biology"
    ["due_date"]=>
    string(10) "2011-01-27"
    ["post_date"]=>
    string(10) "2011-01-26"
    ["status"]=>
    NULL
    ["description"]=>
    string(20) "You must kill a frog"
    ["policy"]=>
    string(48) "Instant death = 100points. Slow death = 40points"
    ["teacher"]=>
    string(9) "Mr. Mihai"
  }
}

以下是$ result-> __ toString在查询的zend中返回:

 SELECT `a`.`id`, 
    `a`.`assignment_name`, 
    `c`.`class_name`, 
    `a`.`due_date`, 
    `a`.`post_date`,
     (a.id = sa.aid) AS `status`, 
    IF(a.description="",'No description given',a.description) AS `description`, 
    IF(a.grading="",'No grading policy',a.grading) AS `policy`, 
    CONCAT(u.prefix,' ',u.lname) AS `teacher` FROM `assignments` AS `a`
INNER JOIN `classes` AS `c` ON a.class_id = c.id
INNER JOIN `enrollments` AS `e` ON e.student_id = 9 AND e.approved = 1 AND c.id = e.class_id
INNER JOIN `users` AS `u` ON u.id = a.teacher_id
LEFT JOIN `student_assignments` AS `sa` ON a.id = sa.id 
GROUP BY `a`.`id` 
ORDER BY `a`.`due_date` 
DESC LIMIT 20

如您所见,此处的状态为NULL。为什么会这样?

1 个答案:

答案 0 :(得分:1)

您的Zend查询缺少IFNULL函数调用。它应该是:

'status' => "IFNULL(a.id = sa.aid,0)",

由于您使用的是LEFT JOIN,如果没有sa个recrod,sa.aid将为空,any number = NULL将返回NULL

我注意到的另一点是,在您的查询中,您正在加入a.id = sa.aid,但在zend查询中,您正加入'a.id = sa.id'(而不是缺少 a )。< / p>