具有连接4表

时间:2016-08-23 02:46:00

标签: php mysql sql codeigniter

伙计我有4个表uhd_voucher_menuuhd_menuuhd_voucher_variant_menuuhd_variant_menu

uhd_voucher_menuuhd_menumenu_id连接,而uhd_voucher_variant_menuuhd_voucher_menuvoucher_menu_iduhd_variant_menu连接,{{} 1}}和voucher_variant_menuvariant_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数据不会调用两次

伙计们,你能帮助我如何执行我想要的数据吗? 谢谢(:

1 个答案:

答案 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();

我们可以在查询输出后将逗号分隔值转换为数组。

尝试并发布结果......