duanfanta6741 2011-02-17 07:38
浏览 40
已采纳

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

I am trying to perform this sql query in zend:

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;

my zend code is this:

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;
    }

I am calling this from my controller as follows:

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

When i perform this query in navicat (the first code paragraph) i get these results:

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

All is fine, that is the result that i want to get. Look at STATUS column (i've put !! to emphasize it). For one result STATUS = 0 and for the other STATUS = 1. This is what it should return. However if i perform the same query using the above code (second code paragraph) i get the following:

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"
  }
}

Here is what $result->__toString returns in zend for the query:

 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

As you can see, status is NULL here. Why is this happening?

  • 写回答

1条回答 默认 最新

  • douda5227 2011-02-17 08:11
    关注

    Your Zend query is missing the IFNULL function call. It should be:

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

    Since you are using a LEFT JOIN, if there are no sa recrods, sa.aid will be null, and any number = NULL will return NULL.

    Another point i noticed, In your query you are joining on a.id = sa.aid, but in the zend query you are joining on 'a.id = sa.id' (not the missing a).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?