Codeigniter join query multiple conditions don't work

前端 未结 6 1271
温柔的废话
温柔的废话 2021-01-14 11:12

I want to select data from my database table with join query, but my it doesn\'t work.

My query:

$this->db->select();
$this->db->from(\'w         


        
6条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-14 11:27

    Two possible problems, depending on what your desired outcome is:

    If you need to make two joins and are getting an error with the second join clause, try using double quotes to enclose the constant value on the condition or you'll get a parse error:

    $this->db->join('schedule', 'schedule.itemtype = "testitem"');
    

    If you need to join only once with multiple conditions, use parentheses:

    $this->db->select('*');
    $this->db->from('we');
    $this->db->join('schedule', '(schedule.itemid = we.cid AND schedule.itemtype="testitem")');
    $this->db->where('we.isActive','Y');
    

    You query is equivalent to writing:

    select * from we
    inner join schedule on schedule.itemid = we.cid
    inner join schedule on schedule.itemtype = "testitem"
    where we.isActive = 'Y'
    

    but what you seem to need is

    select * from we
    inner join schedule on (schedule.itemid = we.cid AND schedule.itemtype = "testitem")
    where we.isActive = 'Y'
    

    On your original query, you are doing two joins. In the latter, you'll do only one with multiple conditions.

提交回复
热议问题