简体   繁体   English

内部联接与codeigniter重复结果

[英]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. 我有以下代码,我正在尝试从大约4个不同的表中获取基于一个订单号的信息。 I am using codeigniter, and with the following code I get a duplicate of the same record. 我正在使用codeigniter,并使用以下代码获得相同记录的副本。 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. 我从codeigniters在线用户手册中获得了此代码。 The only line I added myself was the where and the return line. 我自己添加的唯一一条线是wherereturn线。

To Debug enable PROFILER in your controller's constructor. Debug ,请在控制器的构造函数中启用PROFILER

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

This will display all details below your VIEW including the queries executed.Use that query in phpmyadmin and debug. 这将显示你的下面的所有细节VIEW ,包括queries executed.Use在phpMyAdmin和调试该查询。

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.) (或者每个选项。那些看起来像多对多的连接表。我假设状态ID是唯一的,所以可能不是它。)

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();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM