简体   繁体   中英

CodeIgniter DB get_where and join

is this the best way to write this codeigniter query?

$where = array('registration_status_fk' => 2, 'membership.membership_type_id' => 4);
$join = array('membership', 'membership.id=members.id');

$query = $this->join($join[0], $join[1])->get_where($this->tbl_name, $where);

or is there a better way to accomplish what I am doing?

This is the error I get

Error Number: 1054

Unknown column 'membership.membership_type_id' in 'where clause'

SELECT * FROM (`member`) WHERE `registration_status_fk` = 2 AND `membership`.`membership_type_id` = 4

Filename: C:\xampp\htdocs\OAWA\system\database\DB_driver.php

Line Number: 330

You can write join query this way.

$this->db->select('*')->from('members')->join('membership', 'membership.id=members.id')->where($where)->get();

In $where array you need table name with column name or it may not work if both tables have same column.

You are so close.

I used your example to construct my own join / get_where statement, which is producing correct results:

    $where = array('as_id' => $id);
    $join = array('as_types', 'as_types.as_type_id=assets.as_type');
    $query = $this->db->join($join[0], $join[1])->get_where('assets', $where);

Re-write your query this way:

$where = array('registration_status_fk' => 2, 'membership.membership_type_id' => 4);
$join = array('membership', 'membership.id=members.id');
$query = $this->join($join[0], $join[1])->get_where('members', $where);

Notice the only change is in line 3: $this->tbl_name should be changed to a simple string, 'members'

I used to like this it was working

$where = array('registration_status_fk' => 2, 'ms.membership_type_id' => 4);
$join = array('membership', 'membership.id=members.id');

$query = $this->join('membership ms', 'ms.id = m.id')->get_where('member m', $where);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM