N
N
nethero2015-11-13 21:44:36
CodeIgniter
nethero, 2015-11-13 21:44:36

Codeigniter3, how to select info from three tables in one query?

Hello, need help!
There are three tables
orders
products
alias
You need to select all records from orders, add all records from alias by id (by alias_id which is in orders) + add info from products by orders_id (which is in orders - id )
I try like this

public function test()
        {
            $query = $this->db->select('*')
                    ->from('products')
                    ->join('orders', 'order_id = order_id')
                    ->join('alias', 'alias_id = alias_id')
                    ->get();

            print_r($query->result_array());

        }

output
Error Number: 1052

Column 'alias_id' in on clause is ambiguous

SELECT * FROM `scm_products` JOIN `scm_orders` ON `order_id` = `order_id` JOIN `scm_alias` ON `alias_id` = `alias_id`

Filename: C:/Users/user/Dropbox/MAMPfiles/scm.loc/web/application/models/Buyer_model.php

Line Number: 36

confused HELP!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vyacheslav, 2015-11-13
@nskarl

public function test()
        {
            $query = $this->db->select('p.*' )
                    ->from('products p')
                    ->join('orders o', 'o.order_id = p.order_id')
                    ->join('alias a', 'a.alias_id = p.alias_id')
                    ->get();

            print_r($query->result_array());

        }

D
dmitriy, 2015-11-13
@dmitriylanets

field alias_id to which table does it belong?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question