【问题标题】:how to pass variable into conditional join in codeigniter?如何将变量传递给codeigniter中的条件连接?
【发布时间】:2013-08-28 06:28:10
【问题描述】:

如果我的英语不好,请提前道歉

首先我有这样的变量

$data[code] = "xyz123";
$data[type] = "train";

以及我在这样的模型中编写的查询

$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2', 'table2.id = table1.id');
$this->db->select('table3', 'table3.id = table1.id AND table1.code LIKE $data[code] AND table1.type LIKE $data[type]');

但是当我尝试时,它不起作用,我认为我写的语法 AND 和 LIKE 是错误的。请问有人知道解决办法吗?

谢谢你。

【问题讨论】:

  • 使用" 而不是'。例如"table3.id = table1.id AND table1.code LIKE $data[code] AND table1.type LIKE $data[type]" 而不是 'table3.id = table1.id AND table1.code LIKE $data[code] AND table1.type LIKE $data[type]'
  • 另外,在引用数组键时,使用'"。例如$data['code']$data["code"]
  • 在普通查询中通过变量来查询像这样"table3.id = $data['type']" 对吗?所以在活动记录中我可以这样写?
  • @KhalifaEsha 你检查答案了吗?
  • 因为项目正在进行中,所以我使用的是原始查询,所以我没有尝试过但是请问,当像上面join之后的事件时,@987654333的条件是什么@或者OR,或者我能不能自己决定?

标签: php sql codeigniter join


【解决方案1】:

我觉得你需要试试这个:

$this->db->select('table1.*,table2.*,table3.*');
$this->db->from('table1');
$this->db->join('table2', 'table2.id = table1.id');
$this->db->join('table3', 'table3.id = table1.id');
$this->db->like('table1.type', $data['type']);
$this->db->like('table1.code', $data['code']);

【讨论】:

    【解决方案2】:

    试试类似 CI 的功能:

    $this->db->like('type', $data['type']);
    $this->db->like('code', $data['code']);
    

    【讨论】:

      【解决方案3】:
      $data["code"] = "xyz123";
      $data["type"] = "train";
      
      $this->db->select('*');
      $this->db->from('table1');
      $this->db->join('table2', 'table2.id = table1.id');
      $this->db->select('table3', 'table3.id = table1.id AND table1.code LIKE '.$data["code"].' AND table1.type LIKE '.$data["type"].'');
      

      【讨论】:

        猜你喜欢
        • 2011-06-29
        • 1970-01-01
        • 1970-01-01
        • 2011-09-05
        • 2016-02-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-13
        相关资源
        最近更新 更多