gpt4 book ai didi

php - Codeigniter:MySQL Where 子句和引号

转载 作者:行者123 更新时间:2023-11-29 05:00:57 26 4
gpt4 key购买 nike

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

相反。

关于php - Codeigniter:MySQL Where 子句和引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/727022/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com