簡體   English   中英

Codeigniter:MySQL Where子句和引號

[英]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_atpdb_comments section_idpdb_comments submittedby_idpdb_users usernamepdb_comments textpdb_sections name FROM( pdb_commentspdb_userspdb_sections )WHERE pdb_comments submittedby_id ='users.user_id'AND pdb_comments section_id ='sections.id'ORDER BY pdb_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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM