【问题标题】:Codeigniter Active records complex queryCodeigniter Active 记录复杂查询
【发布时间】:2023-03-24 20:54:01
【问题描述】:

> *1.我需要在活动记录中写这个 *

我需要加入这3张表,但是加入的条件非常有选择性

$this->db->select(name);
$this->db->from('table0');
$this->db->join('table1','(table1.id=0 AND table0.feild1 = table1.feild1) OR (table1.id=1 AND table0.feild2 = table1.feild2)') // <--- how to write this is my question

我可以做一个简单的连接,但主要问题是实现我上面提到的连接条件。这也是非常非常的一小部分!大查询,所以我真的无法将其更改回本机 sql 查询,例如:

$this->db->query('//entire sql query'); //cant do this, need to write ACTIVE RECORDS

当我写活动记录时,萤火虫给我一个错误说:

您的 SQL 语法有错误;检查手册 对应于您的 MySQL 服务器版本,以便使用正确的语法 ') 或

有什么建议吗?

【问题讨论】:

    标签: mysql codeigniter activerecord


    【解决方案1】:

    说实话,我不知道这是可能的。 CI 不使用真正的 Active Records,因此每种方法都有非常严格的参数,我看不出有任何方法可以被欺骗来执行您需要的任务。

    我会尝试重新编写您的查询:

    $this->db->select(name);
    $this->db->from('table0');
    $this->db->join('table1','(table1.id=0 AND table0.feild1 = table1.feild1)', LEFT);
    $this->db->join('table1','(table1.id=1 AND table0.feild2 = table1.feild2)', LEFT);
    $this->db->where('(table1.id=0 AND table0.feild1 = table1.feild1)');
    $this->db->or_where('(table1.id=1 AND table0.feild2 = table1.feild2)');
    

    我相信这个查询会返回正确的值,但您肯定会想要彻底测试它。希望这至少可以为您指明正确的方向。

    【讨论】:

      猜你喜欢
      • 2013-04-11
      • 1970-01-01
      • 2014-04-04
      • 2019-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多