我有一个使用MySQL使用CodeIgniter生成的数组. [co_name] =>中的值;重复,我尝试使用co_id将其group_by使用,但它无法正常工作,但也使用order_id进行group_by.CodeIgnitergroup_by仅适用于一个group_by或任何其他方法,可从[co_name] =>中删除重复的值.
Array
(
[0] => stdClass Object
(
[order_date] => 1408255199
[cus_id] => 6
[order_deliver_name] => Jankia
[order_deliver_add_no] => 5085
[order_deliver_add_street] => mapanawathura road
[order_deliver_add_city] => Kandy
[order_deliver_contact] => 0716352632
[order_deliver_date] => 2014-08-12
[order_status] => pending
[emp_id] =>
[cmp_name] => Burger Bun
[co_name] => Put Ham
Put Tomato
Put Ham
Put Tomato
Put Chilli Sauce
Put Tomato
Put Chilli Sauce
Put Ham
Put Chilli Sauce
[order_id] => 28
[quantity] => 10
)
这是我的代码
function get_customize_orders(){
$this->db->select(array(
'tbl_order_product_details.order_date',
'tbl_order_product_details.cus_id',
'tbl_order_product_details.order_deliver_name',
'tbl_order_product_details.order_deliver_add_no',
'tbl_order_product_details.order_deliver_add_street',
'tbl_order_product_details.order_deliver_add_city',
'tbl_order_product_details.order_deliver_contact',
'tbl_order_product_details.order_deliver_date',
'tbl_order_product_details.order_status',
'tbl_order_product_details.emp_id',
'tbl_customise_main_product.cmp_name',
'GROUP_CONCAT(tbl_customise_optional.co_name SEPARATOR "<br />" ) as co_name ',
'tbl_order_products.order_id',
'tbl_order_products.quantity',
));
$this->db->from('tbl_order_products');
$this->db->join('tbl_customise_join_products', 'tbl_customise_join_products.cmp_id=tbl_order_products.product_id');
$this->db->join('tbl_customise_main_product', 'tbl_customise_main_product.cmp_id=tbl_customise_join_products.cmp_id');
$this->db->join('tbl_customise_order_products', 'tbl_customise_order_products.order_id=tbl_order_products.order_id');
$this->db->join('tbl_customise_optional', 'tbl_customise_optional.co_id=tbl_customise_order_products.co_id');
$this->db->join('tbl_order_product_details', 'tbl_order_product_details.order_id=tbl_order_products.order_id');
$this->db->group_by('tbl_order_products.order_id');
$query = $this->db->get();
return $query->result();
}
解决方法:
在GROUP_CONCAT中使用DISTINCT,以便每个order_id组仅连接不同的co_name
GROUP_CONCAT(DISTINCT tbl_customise_optional.co_name SEPARATOR "<br />" ) as co_name