dongsheng8158 2013-11-22 04:58
浏览 30
已采纳

数据库查询未正确检索

I am using zend framework and my sql to display some data.

Query is given below:

    $table = new Timesheets_Table_Tasks();
    $select = $table->select(true)
                ->setIntegrityCheck(false)
                ->join('timesheets_projects', 'timesheets_projects.ID=timesheets_tasks.ProjectID', array())
                ->join('timesheets_clients', 'timesheets_clients.ID=timesheets_projects.ClientID', array())


    return $this->filterSelect($select);

And the function filterSelect is given below:

protected function filterSelect($select)
    {
        $select->where("timesheets_tasks.UserID=?", Zend_Auth::getInstance()->getIdentity()->id);
        foreach($this->_filters as $filter => $value)
        {
            if(($value == "" || $value == "none") && $filter != "status")
            {
                continue;
            }

            switch($filter)
            {
                case 'q':
                    $select->where($this->_db->quoteInto('timesheets_tasks.Comment LIKE ?', '%' . $value . '%'));
                    break;
                case 'after':
                    $select->where($this->_db->quoteInto('timesheets_tasks.Date >= UNIX_TIMESTAMP(STR_TO_DATE(?, "%d/%m/%Y"))', $value));
                    break;
                case 'before':
                    $select->where($this->_db->quoteInto('timesheets_tasks.Date <= UNIX_TIMESTAMP(STR_TO_DATE(?, "%d/%m/%Y"))', $value));
                    break;
                case 'client':
                    $select->where($this->_db->quoteInto('timesheets_clients.ID = ?', $value));
                    break;
                case 'project':
                    $select->where($this->_db->quoteInto('timesheets_tasks.ProjectID = ?', $value));
                    break;
                case 'status':
                    $select->where($this->_db->quoteInto('timesheets_tasks.Status = ?', $value));
                    break;
                case 'jobtype':
                    $select->where($this->_db->quoteInto('timesheets_tasks.TypeID = ?', $value));
                    break;
            }
        }

        return $select;
    }

I displayed the query and it displays as:

SELECT timesheets_tasks.* FROM timesheets_tasks INNER JOIN timesheets_projects ON timesheets_projects.ID=timesheets_tasks.ProjectID INNER JOIN timesheets_clients ON timesheets_clients.ID=timesheets_projects.ClientID WHERE (timesheets_tasks.UserID='33') AND (timesheets_tasks.Status = 0) ORDER BY date DESC

But the query doesn't give the actual data.

I think it is an issue with join tables. When I removed the line " ->join('timesheets_clients', 'timesheets_clients.ID=timesheets_projects.ClientID', array())" from the query, the output showed correctly. But I have to select some of the field from the other two tables too. So I need a join property. Any other join can I use to get the column values from the other two tables and get the correct output?

I am new to joins. Any help should be appreciated!

  • 写回答

1条回答 默认 最新

  • douruyun8153 2013-11-22 05:51
    关注

    Could be help you to resolve

      $table = new Timesheets_Table_Tasks();
        $select = $table->select(true)
                    ->setIntegrityCheck(false)
                    ->joinLeft('timesheets_projects', 'timesheets_projects.ID=timesheets_tasks.ProjectID', array())
                    ->joinLeft('timesheets_clients', 'timesheets_clients.ID=timesheets_projects.ClientID', array())
    
    
        return $this->filterSelect($select)
    

    Also please check with echo your select statement with join

    hope this will work for you.

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

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用