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

How to define where condition (ex: userid != 0) in this below query?

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

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

您可以通过在查询开始前检查条件来准备 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)"

这是您需要的东西还是不同的东西?