1

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 リクエストの生成:

選択しpdb_commentsます。created_atpdb_commentssection_idpdb_commentssubmittedby_idpdb_usersusernamepdb_commentstextpdb_sectionsnameFROM ( pdb_comments, pdb_users, pdb_sections) WHERE pdb_comments. submittedby_id= 'users.user_id' AND pdb_comments. section_id= 'sections.id' ORDER BY pdb_comments. created_atdesc LIMIT 10

問題は、データベース プレフィックス ( ) が句pdb_に追加されないことです。WHEREを追加してプレフィックスを手動で挿入できます$this->db->dbprefixが、これでは主な問題は解決しません。

引用符:

`pdb_comments`.`submittedby_id` = 'pdb_users.user_id'

右側の引用符は正確ではなく、結果は 0 です。CodeIgniter に where 句の後半をテーブルの一部として認識させる方法はありますか? これにより、データベースのプレフィックスを追加し、2 つの結合を回避して引用符を適切に配置しますか? これを行う別の方法はありますか?前もって感謝します。

--

ジョン

4

2 に答える 2

3

使用する:

$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();

代わりは。

于 2009-04-07T19:15:22.463 に答える
1

ばかげた質問かもしれませんが、SQL を直接記述してみませんか? インターフェースは、ごちゃごちゃしているようには見えません。

于 2009-04-07T18:37:01.027 に答える