[SOLVED] Joining Information from two database using Active Record - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: [SOLVED] Joining Information from two database using Active Record (/showthread.php?tid=40022) |
[SOLVED] Joining Information from two database using Active Record - El Forum - 03-27-2011 [eluser]tkaw220[/eluser] I need assistance on below issue: Said I have 2 table, one named 'orders' storing order no, currency, shipping and billing info, purchased date ... Another table named 'order_items' storing order no, and items purchased with same order_no. Below is my code: function get_order($order_no) { $this->db->select('order_no, item, qty, currency, price, subtotal'); $this->db->from('order_items'); $this->db->where('order_no', $order_no); $this->db->join('orders', "orders.order_no = $order_no"); $query = $this->db->get(); } My above code return error: Column 'order_no' in field list is ambiguous What is the proper code to join these two tables? [SOLVED] Joining Information from two database using Active Record - El Forum - 03-28-2011 [eluser]CroNiX[/eluser] Quote:My above code return error: Column ‘order_no’ in field list is ambiguousThat usually means you have the same field name in both tables, and you are doing a join so it doesn't know which field from which table you are referring to. If you are only using 1 table in a query, it is implied that you want the data from that table so you don't need to be explicit. In your selects and wheres, change them to "table.field" instead of just "field". your join would probably look like: Code: $this->db->join('orders', 'orders.order_no = order_items.order_no'); I usually use multiple selects if I am doing a join (one for each table)... If you have fields named the same in both tables you will also need to select them AS something else or there will be an error because you will get two columns returned with the same name, which can't happen. Code: $this->db->select('order_items.order_no AS order_no, order_items.item AS item, order_items.qty AS qty');//get order_no, item and qty from order_items Now your result will have a row with: Code: $row->order_no; //from order_items [SOLVED] Joining Information from two database using Active Record - El Forum - 03-28-2011 [eluser]tkaw220[/eluser] Hi CroNiX, Very useful information. Thanks. [SOLVED] Joining Information from two database using Active Record - El Forum - 07-22-2012 [eluser]anyamanggar[/eluser] i have sam problem here Code: function cariinfo($keyword) { [quote] Error Number: 1052 Column 'title' in field list is ambiguous SELECT `title` FROM (`info`) JOIN `berita` ON `info`.`id` = `berita`.`id` WHERE `info`.`title` LIKE ' |