简体   繁体   中英

inner join with codeigniter duplicating results

I have the following code and I'm trying to get information based on one order number from about 4 different tables. I am using codeigniter, and with the following code I get a duplicate of the same record. Basically two rows are displayed when only one truly exists in the database.

function get_orders($user_id)
{
    $this->db->select('*');
    $this->db->from('Orders');
    $this->db->join('Order_Options', 'Orders.orderNumber = Order_Options.orderNumber');
    $this->db->join('Order_Products', 'Orders.orderNumber = Order_Products.orderNumber');
    $this->db->join('Order_Status', 'Orders.order_status = Order_Status.id');
    $this->db->where(array('user_id' => $user_id));

    $query = $this->db->get();
    return $query->result();
}

I got this code from the codeigniters online user manual. The only line I added myself was the where and the return line.

To Debug enable PROFILER in your controller's constructor.

$this->output->enable_profiler(TRUE);

This will display all details below your VIEW including the queries executed.Use that query in phpmyadmin and debug.

I don't know this tool, but this should return a separate record for each product in the order. (Or each option. Those look like many-to-many connection tables. I assume status IDs are unique, so that's probably not it.)

What I would do to debug is start simplifying the query, and then I could see which part causes the duplication.

Check this

function get_orders($user_id)
    {
        $query=$this->db->select('*');
        ->distinct()
        ->from('Orders');
        ->join('Order_Options', 'Orders.orderNumber = Order_Options.orderNumber');
        ->join('Order_Products', 'Orders.orderNumber = Order_Products.orderNumber');
        ->join('Order_Status', 'Orders.order_status = Order_Status.id');
        ->where(array('user_id' => $user_id));
        ->get();
        return $query->result();
    }

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