Codeigniter查询生成多行

时间:2017-09-29 10:36:57

标签: php mysql codeigniter

笨:

public PinLoginPageModel()
    {

        NumberCommand = new Command<string>(NumpadToNumber);
    }

    void NumpadToNumber(string obj)
    {
        if(obj == "0")
        {
            PincodeMasked = "0";
        }
    }

在customer表中,有2列连接到同一个表。在customer表中,只有1行。但是当查询生成它时会生成1000多行。任何人都可以帮助我,为什么它生成1000多行,我该如何解决它。

2 个答案:

答案 0 :(得分:0)

您始终可以使用$this->db->last_query()直观地了解和调试代码。这样做很好,因为您可以开始了解Active Record的工作原理。

$results = $this->db->get()->result_array();
log_message( "debug", "get_customers sql: ". $this->db->last_query() );
return $results;

所以当你以原始格式看到你的查询时;它看起来像这样:

SELECT 
    *,customer.ID as ID,
    customer.Create_date as Create_date,
    transport.Name as Transporter, 
    state.Name as State, 
    city.Name as City, 
    area.Name as Area, 
    shipping_state.Name as Shipping_state, 
    shipping_city.Name as Shipping_city, 
    shipping_area.Name as Shipping_area
FROM
    customer
LEFT JOIN transport ON ( 
    transport.ID = customer.Transporter
)
LEFT JOIN state ON ( 
    state.ID = customer.State
)
LEFT JOIN state as shipping_state ON ( 
    state.ID = customer.Shipping_state
)
LEFT JOIN city as shipping_state ON ( 
    city.ID = customer.City
)
LEFT JOIN city as shipping_city ON ( 
    city.ID = customer.Shipping_city
)
LEFT JOIN area ON ( 
    area.ID = customer.Area
)
LEFT JOIN area as shipping_area ON ( 
    area.ID = customer.Shipping_area
)

这里有很多左连接;它会在每个左连接上重复任何匹配的行。在不知道数据的情况下很难说,但似乎您更愿意使用硬连接。

答案 1 :(得分:0)

根据你拥有的联接数量,你可能需要通过

添加一个组

所以在你加入之前:

$this->db->group_by('customer.ID');