简体   繁体   中英

Join query codeigniter with where condition returns all rows

Here is my join query for getting data from two tables.

$this->db->select('*');
$this->db->from('george_hotel_bkd_customers');
$this->db->where('george_hotel_bookings.bookingref',$ref);
$this->db->join('george_hotel_bookings', 'george_hotel_bookings.user_id = george_hotel_bkd_customers.user_id');    
$query = $this->db->get();

According to my where condition it returns only one row but it will returns all the rows with matches the join condition.

Seems like my where condition is not executed here.

please help me

What does $ref =, how many results should it pick up roughly? what do you get when you omit the where? if you get full result then must be an issue with $ref value, or, that is the result.

Just a note, you don't need to select(*), this is default, if this is an just for examples sake, sorry for picking up. You can also add the ->from(whaterver_table) to ->get(whatever_table) You need to add ->get() anyway, so why not remove the ->from() line, just a choice of readability, but i did not realise you could do this for a while, so thought id add it to my answer.

Or another problem solving path is whether the join should be 'left' as 3rd arg. IE is there always a join?

Altering the position or the join in the statement would not make any difference, as Anant suggested

$this->db->select('*');
$this->db->from('george_hotel_bkd_customers');
$this->db->join('george_hotel_bookings', 'george_hotel_bookings.user_id = george_hotel_bkd_customers.user_id');
$this->db->where('george_hotel_bookings.bookingref',$ref);
$query = $this->db->get();

always where is last

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