2013-05-17 49 views
0

請幫我解決這個問題..笨在查詢添加額外的錯誤報價

我要去的MySQL做查詢,但代碼點火器讀我的格式錯誤的查詢:

我的查詢:

$this->db->select('substring(sbj_prog,1,5) as prog , count(sbj_prog) as bil'); 
$this->db->from('sbj_app'); 
$this->db->group_by('prog'); 
$this->db->limit('10'); 

這是多麼笨讀

SELECT substring(sbj_prog, `1`, `5)` as prog, count(sbj_prog) as bil FROM (`sbj_app`) GROUP BY `prog` LIMIT 10 

問題是爲什麼多餘的報價被添加到5)

非常感謝。

回答

0

你可以嘗試額外的參數假像這樣

$this->db->select("substring(sbj_prog,1,5) as prog , count(sbj_prog) as bil", FALSE); 

,將工作

0

嘗試,你得到像

$this->db->select('sbj_prog as prog , count(sbj_prog) as bil'); 
$this->db->from('sbj_app'); 
$this->db->group_by('prog'); 
$this->db->limit('10'); 

那麼結果之後添加子你GOR結果嘗試後使用子字符作爲

$prog = substring($row['prog'],1,5); 

,你可以使用

$this->db->select('substring(sbj_prog,1,5) as prog,count(sbj_prog) as bil',FALSE); 

請參閱本Select

0

CodeIgniter's activerecord手冊;

$ this-> db-> select()接受可選的第二個參數。 如果將其設置爲FALSE,CodeIgniter將不會嘗試使用反引號來保護字段或表名。如果您需要複合選擇語句,這很有用。

換句話說,如果你的情況你只有常量,你可以使用;

$this->db->select('substring(sbj_prog,1,5) as prog , count(sbj_prog) as bil', 
        FALSE);