如何在下面的查询中定义where条件(例如:userid!= 0)?

时间:2017-01-18 12:14:33

标签: mysql joomla

如何在下面的查询中定义条件?形成数据库,除了需要显示的零值

 private function totalVisitedPagesPerUser() {
            $query = "SELECT COUNT(*), " . $this->_db->quoteName('customer_name') . "," . 
                     "\n MAX(". $this->_db->quoteName('visit_timestamp') . ")," .                        $this->_db->quoteName('browser') . "," .
                     $this->_db->quoteName('os') . "," .
                     $this->_db->quoteName('session_id_person') . "," . 
                     $this->_db->quoteName('ip') . "," .
                     "\n SUM(" . $this->_db->quoteName('impulse') . ")," .
                     $this->_db->quoteName('geolocation') .
                     "\n FROM ( SELECT * FROM #__realtimeanalytics_serverstats " . $this->whereQuery . " ORDER BY  `visit_timestamp` DESC) AS INTABLE" .
                     $this->whereQuery .
                     "\n GROUP BY " . $this->_db->quoteName('session_id_person') .
                     "\n ORDER BY " . $this->_db->quoteName('customer_name');
            $this->_db->setQuery($query);
            $results = $this->_db->loadRowList();
            if ($this->_db->getErrorNum()) {
                throw new JRealtimeException(JText::sprintf('COM_JREALTIME_ERROR_RECORDS', $this->_db->getErrorMsg()), 'error');
            }
            return $results;
        }

1 个答案:

答案 0 :(得分:0)

您可以通过在查询开始之前检查条件来准备where变量,而不是在该点附加该变量,如:

$wherecondtion = "Where 1=1";
if($condition1){$wherecondtion."AND (add required filter 1)"}
if($condition2){$wherecondtion."AND (add required filter 2)"}

您的子查询将如下:

"( SELECT * FROM #__realtimeanalytics_serverstats " . $wherecondtion . " ORDER BY  `visit_timestamp` DESC)"

这是你需要的东西还是其他不同的东西?