2016-10-13 156 views
1

我得到這個錯誤,無法弄清楚怎麼回事,請幫忙。SQL語法錯誤

數據庫出錯

錯誤編號:1064

您的SQL語法錯誤;檢查 對應於你的MySQL服務器版本正確的語法使用 附近 ')' 在行1

SELECT GROUP_CONCAT(DISTINCT ud.userid) as id from pr_users_details ud INNER JOIN pr_users u ON ud.userid = u.id WHERE ud.status = '1' AND() 

我的功能看起來像這樣的手冊:

\t function get_nomination_emailids($functions, $levels, $roles, $locations, $emails) 
 
\t \t { 
 
\t \t \t 
 
\t \t \t $SQL.="SELECT GROUP_CONCAT(DISTINCT ud.userid) as id from pr_users_details ud INNER JOIN pr_users u ON ud.userid = u.id WHERE ud.status = '1' "; 
 
\t \t \t if(count($functions)>0) 
 
\t \t \t { 
 
\t \t \t \t $d = implode(",",$functions); 
 
\t \t \t \t $whereand[] = " u.departmentid IN (".$d.") "; 
 
\t \t \t } 
 
\t \t \t if(count($levels)>0) 
 
\t \t \t { 
 
\t \t \t \t $d1 = implode(",",$levels); 
 
\t \t \t \t $whereand[] = " ud.designation_id IN (".$d1.") "; 
 
\t \t \t } 
 
\t \t \t if(count($roles)>0) 
 
\t \t \t { 
 
\t \t \t \t $d2 = implode(",",$roles); 
 
\t \t \t \t $whereand[] = " u.userroleid IN (".$d2.") "; 
 
\t \t \t } 
 
\t \t \t if(count($locations)>0) 
 
\t \t \t { 
 
\t \t \t \t $d3 = implode(",",$locations); 
 
\t \t \t \t $whereand[] = " u.branchid IN (".$d3.") "; 
 
\t \t \t } 
 
\t \t 
 
\t \t \t if(count($emails)>0) 
 
\t \t \t { 
 
\t \t \t \t $d4 = implode(",",$emails); 
 
\t \t \t \t $whereor[] = " ud.userid IN (".$d4.") "; 
 
\t \t \t } 
 
\t \t \t $whr = array(); 
 
\t \t \t 
 
\t \t \t if(isset($whereand)) 
 
\t \t \t $whr[] = " (".implode(" AND ",$whereand).") "; 
 
\t \t 
 
\t \t \t if(isset($whereor)) 
 
\t \t \t $whr[] = " (".implode(" OR ",$whereor).") "; 
 
\t \t \t 
 
\t \t \t 
 
\t \t \t if(count($whr > 0)) 
 
\t \t \t { 
 
\t \t \t \t 
 
\t \t \t \t $SQL .= " AND (".implode(" OR ",$whr).") "; 
 
\t \t \t } 
 
\t \t \t 
 
\t \t \t $query = $this->db->query($SQL); 
 
\t \t 
 
\t \t \t $return \t = \t $query->result_array(); 
 
\t \t \t 
 
\t \t \t return $return[0]['id']; 
 
\t \t \t //print_r($return);die; 
 
\t \t \t 
 
\t \t }

+5

'AND()'需要包含或刪除它們。 –

+0

Btw'count()'返回每個不是數組或NULL的項的'1'。所以如果'$ emails'或'$ locations'是__FALSE__ count是1。 –

回答

-1

AND()在您的查詢中刪除試試這個。

SELECT GROUP_CONCAT(DISTINCT ud.userid) as id from pr_users_details ud INNER JOIN pr_users u ON ud.userid = u.id WHERE ud.status = '1'