伙计我有4个表uhd_voucher_menu
,uhd_menu
,uhd_voucher_variant_menu
,uhd_variant_menu
uhd_voucher_menu
和uhd_menu
由menu_id
连接,而uhd_voucher_variant_menu
和uhd_voucher_menu
由voucher_menu_id
和uhd_variant_menu
连接,{{} 1}}和voucher_variant_menu
由variant_menu_id
这是我的代码如何获取数据:
$this->db->select("uhd_voucher_menu.voucher_id,uhd_menu.menu_name,uhd_voucher_menu.qty,uhd_variant_menu.variant_name,uhd_voucher_menu.voucher_menu_id");
$this->db->from("uhd_voucher_menu");
$this->db->join("uhd_menu","uhd_menu.menu_id = uhd_voucher_menu.menu_id");
$this->db->join("uhd_voucher_variant_menu","uhd_voucher_variant_menu.voucher_menu_id = uhd_voucher_menu.voucher_menu_id","left");
$this->db->join("uhd_variant_menu","uhd_variant_menu.variant_menu_id = uhd_voucher_variant_menu.variant_menu_id","left");
$this->db->where("voucher_id",$data["voucher"]["voucher_id"]);
$this->db->where("uhd_menu.restaurant_id",$restaurant_id);
$res = $this->db->get()->result_array();
来自该活跃记录的将返回此数据:
[0]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_name"]=>
string(13) "Golden Salmon"
["qty"]=>
string(1) "2"
["variant_name"]=>
string(5) "Spicy"
["voucher_menu_id"]=>
string(1) "3"
}
[1]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_name"]=>
string(13) "Golden Salmon"
["qty"]=>
string(1) "2"
["variant_name"]=>
string(6) "Medium"
["voucher_menu_id"]=>
string(1) "3"
}
[2]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_name"]=>
string(13) "Golden Salmon"
["qty"]=>
string(1) "1"
["variant_name"]=>
string(4) "Sour"
["voucher_menu_id"]=>
string(1) "4"
}
[3]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_name"]=>
string(13) "Golden Salmon"
["qty"]=>
string(1) "1"
["variant_name"]=>
string(6) "Medium"
["voucher_menu_id"]=>
string(1) "4"
}
[4]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_name"]=>
string(14) "Red Crown Crab"
["qty"]=>
string(1) "2"
["variant_name"]=>
NULL
["voucher_menu_id"]=>
string(1) "1"
}
[5]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_name"]=>
string(8) "Omellete"
["qty"]=>
string(1) "3"
["variant_name"]=>
NULL
["voucher_menu_id"]=>
string(1) "2"
}
如您所见golden_salmon
同一voucher_menu_id
两次返回
我希望如果voucher_menu_id
相同,那么根据这些数据:
[0]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_name"]=>
string(13) "Golden Salmon"
["qty"]=>
string(1) "2"
["variant_name"]=>
string(5) "Spicy"
["voucher_menu_id"]=>
string(1) "3"
}
[1]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_name"]=>
string(13) "Golden Salmon"
["qty"]=>
string(1) "2"
["variant_name"]=>
string(6) "Medium"
["voucher_menu_id"]=>
string(1) "3"
}
我希望数据返回成为:
[0]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_name"]=>
string(13) "Golden Salmon"
["qty"]=>
string(1) "2"
["variant_name"]=>
array(2){
[1]=>
string(5) "Spicy"
[2]=>
string(6) "Medium"
}
["voucher_menu_id"]=>
string(1) "3"
}
与具有相同golden_salmon
voucher_menu_id
4
相同
我希望variant_name
成为数组,因此具有相同voucher_menu_id
的if数据不会调用两次
伙计们,你能帮助我如何执行我想要的数据吗? 谢谢(:
答案 0 :(得分:-1)
你必须使用group by,我稍微修改了你的查询:
$this->db->select("uhd_voucher_menu.voucher_id,uhd_menu.menu_name,uhd_voucher_menu.qty,GROUP_CONCAT(uhd_variant_menu.variant_name SEPARATOR ',') AS variants,uhd_voucher_menu.voucher_menu_id");
$this->db->from("uhd_voucher_menu");
$this->db->join("uhd_menu","uhd_menu.menu_id = uhd_voucher_menu.menu_id");
$this->db->join("uhd_voucher_variant_menu","uhd_voucher_variant_menu.voucher_menu_id = uhd_voucher_menu.voucher_menu_id","left");
$this->db->join("uhd_variant_menu","uhd_variant_menu.variant_menu_id = uhd_voucher_variant_menu.variant_menu_id","left");
$this->db->where("voucher_id",$data["voucher"]["voucher_id"]);
$this->db->where("uhd_menu.restaurant_id",$restaurant_id);
$this->db->group_by("uhd_voucher_menu.voucher_id");
$res = $this->db->get()->result_array();
我们可以在查询输出后将逗号分隔值转换为数组。
尝试并发布结果......