简体   繁体   中英

Codeigniter Active Record “JOIN” with two Index

my tables (structures, surface) have two Index-Rows, "planet_id" and "tile_id". I want to join them, but i get a SQL-Error: "Column 'planet_id' in where clause is ambiguous".

$this->db   ->select('*')
    ->from('structures')
    ->join('surface', 'structures.planet_id=surface.planet_id AND structures.tile_id=surface.tile_id')
    ->where('planet_id', $p->planet_id);

$query = $this->db->get();

Leads to:

Error Number: 1052

Column 'planet_id' in where clause is ambiguous

SELECT * FROM (`structures`) JOIN `surface` ON `structures`.`planet_id`=`surface`.`planet_id` AND structures.tile_id=surface.tile_id WHERE `planet_id` = '13247'

Since you have planet_id in two tables, you'll need to choose which you're applying the where to.

So, try this:

$this->db->select('*')
    ->from('structures')
    ->join('surface', 'structures.planet_id=surface.planet_id AND structures.tile_id=surface.tile_id')
    ->where('structures.planet_id', $p->planet_id);

$query = $this->db->get();

It might seem silly, because your join requires both planet_id 's to be the same, but the where doesn't know that, and needs specific instructions.

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