[英]Codeigniter: MySQL Where Clause and Quotes
在CodeIgniter中查詢 :
$this->db->select('comments.created_at, comments.section_id, comments.submittedby_id, users.username, comments.text, sections.name');
$this->db->order_by('comments.created_at', 'desc');
$this->db->where('comments.submittedby_id', 'users.user_id');
$this->db->where('comments.section_id', 'sections.id');
$query = $this->db->get(array('comments', 'users', 'sections'),10);
生成SQL請求 :
SELECT
pdb_comments
。created_at
,pdb_comments
。section_id
,pdb_comments
。submittedby_id
,pdb_users
。username
,pdb_comments
。text
,pdb_sections
。name
FROM(pdb_comments
,pdb_users
,pdb_sections
)WHEREpdb_comments
。submittedby_id
='users.user_id'ANDpdb_comments
。section_id
='sections.id'ORDER BYpdb_comments
。created_at
desc LIMIT 10
問題是數據庫前綴( pdb_
)沒有添加到WHERE
子句中。 我可以通過附加$this->db->dbprefix
手動插入前綴,但這不能解決主要問題。
行情 :
`pdb_comments`.`submittedby_id` = 'pdb_users.user_id'
右側的引號不准確,並為我生成0結果。 有沒有辦法讓CodeIgniter將where子句的后半部分識別為我的表的一部分; 從而添加數據庫前綴,並通過避免兩個連接正確放置引號? 還有另一種方法嗎? 提前致謝。
-
喬恩
采用:
$this->db->select('comments.created_at, comments.section_id, comments.submittedby_id, users.username, comments.text, sections.name');
$this->db->from('comments');
$this->db->join('users', 'comments.submittedby_id=users.user_id');
$this->db->join('sections', 'comments.section_id=sections.id');
$this->db->order_by('comments.created_at', 'desc');
$query = $this->db->get();
代替。
可能是一個愚蠢的問題,但為什么不直接編寫SQL? 界面看起來不像給你任何東西而是雜亂無章。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.