【问题标题】:Codeigniter query builder does not work correctlyCodeigniter 查询生成器无法正常工作
【发布时间】:2020-04-18 22:04:03
【问题描述】:

有人可以解释这些代码之间的区别吗? 因为查询生成器没有给出正确的结果,但是另一个查询给出了正确的结果。

我看不到的区别是什么?

$this->db->select('m.*,c.COUNTRY_NAME');
$this->db->from('members m');
$this->db->join('country c','c.COUNTRY_ALPHA2_CODE = m.location', 'left');
$this->db->where('c.LANG', 'EN');

给出正确结果的查询

SELECT m.*,c.COUNTRY_NAME FROM members m LEFT JOIN country c ON c.COUNTRY_ALPHA2_CODE = m.location WHERE c.LANG = "EN"; 

【问题讨论】:

    标签: php codeigniter codeigniter-query-builder


    【解决方案1】:

    要使用 CI 生成完整的查询字符串,您需要添加以下行:

    $query=$db->get(); 你的方法。

    完整的代码如下所示:

    $this->db->select('m.*,c.COUNTRY_NAME');
    $this->db->from('members m');
    $this->db->join('country c','c.COUNTRY_ALPHA2_CODE = m.location', 'left');
    $this->db->where('c.LANG', 'EN');
    $query=$db->get();
    

    在此行之后,您可以使用以下命令检查 SQL 字符串输出:

    echo $this->db->query();
    

    从这里你继续Generating Query Results 为你的意见

    回复评论:

    '$this->db->where('c.LANG', 'EN');' 不起作用。此行返回 永远是 db 中的第一语言

    您需要将语言查询放入连接中:

    $this->db->select('m.*,c.COUNTRY_NAME');
    $this->db->from('members m');
    $this->db->join('country c','(c.COUNTRY_ALPHA2_CODE = m.location AND c.LANG=\'EN\')', 'left');
    $query=$db->get();
    

    【讨论】:

      【解决方案2】:

      试试这个:

      $this->db->select('m.*');
      $this->db->select('c.COUNTRY_NAME');
      $this->db->from('members m');
      $this->db->join('country c','c.COUNTRY_ALPHA2_CODE = m.location', 'left');
      $this->db->where('c.LANG', 'EN');
      

      【讨论】:

      • '$this->db->where('c.LANG', 'EN');'不起作用。此行始终返回 db 中的第一语言
      • 请解释为什么这段代码会产生与 OP 中不同的 SQL 查询?你可以echo $this->db->query();,会看到没有区别;
      猜你喜欢
      • 2015-01-12
      • 1970-01-01
      • 1970-01-01
      • 2014-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-06
      • 1970-01-01
      相关资源
      最近更新 更多