Codeigniter:
public function get_customers(){
$this->db->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');
$this->db->from('customer');
$this->db->join('transport','transport.ID = customer.Transporter','LEFT');
$this->db->join('state','state.ID = customer.State','LEFT');
$this->db->join('state as shipping_state','state.ID = customer.Shipping_state','LEFT');
$this->db->join('city','city.ID = customer.City','LEFT');
$this->db->join('city as shipping_city','city.ID = customer.Shipping_city','LEFT');
$this->db->join('area','area.ID = customer.Area','LEFT');
$this->db->join('area as shipping_area','area.ID = customer.Shipping_area','LEFT');
return $this->db->get()->result_array();
}
In customer table, there are 2 columns joining to the same table. in the customer table, there are only 1 rows. but when the query is generating it generating 1000+ rows. Anyone can please help me why it generating 1000+ rows and How can I resolve it.
You can always use $this->db->last_query()
to visually understand and debug your code. It's good to do this, as you then you can start to understand how Active Record works.
$results = $this->db->get()->result_array();
log_message( "debug", "get_customers sql: ". $this->db->last_query() );
return $results;
So when you see your query in raw format; it looks like this:
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
)
There's so many left joins here; that it will repeat any matched rows on each left join. It's hard to say without knowing the data, but it would appear that you would prefer to use a Hard Join.
With the amount of joins you have you might have to add a group by
so before your get add:
$this->db->group_by('customer.ID');
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.