如何在codeigniter活动记录中使用oder_by count返回数据

时间:2016-08-08 04:12:47

标签: php mysql codeigniter

我在每个表格中有3个包含restaurant_id的表格,我试图在uhd_restaurant加入uhd_user_orderuhd_order_history

上获取数据

这是我的代码:

$this->db->DISTINCT();
$this->db->select("t1.restaurant_id,t1.restaurant_name,t1.restaurant_code, COUNT(t1.restaurant_id)as total");
$this->db->from("uhd_restaurant as t1");
$this->db->join("uhd_user_order as t2","t1.restaurant_id = t2.restaurant_id","left");
$this->db->join("uhd_order_history as t3","t1.restaurant_id = t3.restaurant_id");
$this->db->group_by("t1.restaurant_name");
$this->db->order_by("total","desc");
$res = $this->db->get()->result_array();
return $res;

该代码将返回:

array(6) {
  [0]=>
  array(4) {
    ["restaurant_id"]=>
    string(3) "365"
    ["restaurant_name"]=>
    string(8) "Yukihira"
    ["restaurant_code"]=>
    string(4) "MG99"
    ["total"]=>
    string(2) "88"
  }
  [1]=>
  array(4) {
    ["restaurant_id"]=>
    string(3) "367"
    ["restaurant_name"]=>
    string(5) "alice"
    ["restaurant_code"]=>
    string(4) "ZF42"
    ["total"]=>
    string(1) "3"
  }
  [2]=>
  array(4) {
    ["restaurant_id"]=>
    string(3) "363"
    ["restaurant_name"]=>
    string(6) "takumi"
    ["restaurant_code"]=>
    string(4) "ZO09"
    ["total"]=>
    string(1) "2"
  }
  [3]=>
  array(4) {
    ["restaurant_id"]=>
    string(3) "368"
    ["restaurant_name"]=>
    string(5) "test1"
    ["restaurant_code"]=>
    string(4) "WS94"
    ["total"]=>
    string(1) "2"
  }
  [4]=>
  array(4) {
    ["restaurant_id"]=>
    string(3) "366"
    ["restaurant_name"]=>
    string(6) "nakiri"
    ["restaurant_code"]=>
    string(4) "XN27"
    ["total"]=>
    string(1) "1"
  }
}

在我的表uhd_user_order中有3个restaurant_id,其中

364有1个,365有8个,366有1个

并在我的uhd_order_history中有4 restaurant_id

365有11个,363有2个,368有2个,367有3个

所以总数应该是

365 = 19,363 = 2,364 = 1,366 = 2 367 = 3且368 = 2 < / p>

但是我返回了错误的数据,请将365的返回总数与88一起查看,但19我的366只返回1而我无法退回restaurant_id 364

所以数据应该返回到:

array(5) {
  [0]=>
  array(4) {
    ["restaurant_id"]=>
    string(3) "365"
    ["restaurant_name"]=>
    string(8) "Yukihira"
    ["restaurant_code"]=>
    string(4) "MG99"
    ["total"]=>
    string(2) "19"
  }
  [1]=>
  array(4) {
    ["restaurant_id"]=>
    string(3) "367"
    ["restaurant_name"]=>
    string(5) "alice"
    ["restaurant_code"]=>
    string(4) "ZF42"
    ["total"]=>
    string(1) "3"
  }
  [2]=>
  array(4) {
    ["restaurant_id"]=>
    string(3) "363"
    ["restaurant_name"]=>
    string(6) "takumi"
    ["restaurant_code"]=>
    string(4) "ZO09"
    ["total"]=>
    string(1) "2"
  }
  [3]=>
  array(4) {
    ["restaurant_id"]=>
    string(3) "368"
    ["restaurant_name"]=>
    string(5) "test1"
    ["restaurant_code"]=>
    string(4) "WS94"
    ["total"]=>
    string(1) "2"
  }
  [4]=>
  array(4) {
    ["restaurant_id"]=>
    string(3) "366"
    ["restaurant_name"]=>
    string(6) "nakiri"
    ["restaurant_code"]=>
    string(4) "XN27"
    ["total"]=>
    string(1) "2"
  }
  [5]=>
  array(4) {
    ["restaurant_id"]=>
    string(3) "364"
    ["restaurant_name"]=>
    string(6) "test"
    ["restaurant_code"]=>
    string(4) "WY58"
    ["total"]=>
    string(1) "1"
  }
}

uhd_restaurant中的P.S restaurant_id uhd_user_orderuhd_order_history

中的所有function executeQuery( stmt, data ) { var outhjk = ""; pool.connect(function(err, client, done) { if(err) { console.error('error fetching client from pool', err); } client.query(stmt, data, function(err, result) { //call `done()` to release the client back to the pool done(); if(err) { return console.error('error running query', err); } outhjk = "just work please"; }); }); return outhjk; }

你可以帮助我如何获得正确的数据总量吗?

2 个答案:

答案 0 :(得分:0)

$this->db->select("t1.restaurant_id,t1.restaurant_name,t1.restaurant_code, COUNT(t1.restaurant_id)as total");
$this->db->from("uhd_restaurant as t1");
$this->db->join("uhd_user_order as t2","t1.restaurant_id = t2.restaurant_id","left");
$this->db->join("uhd_order_history as t3","t1.restaurant_id = t3.restaurant_id");
$this->db->group_by("t1.restaurant_name");
$this->db->order_by("total","desc");
$res = $this->db->get()->result_array();
return $res;

答案 1 :(得分:0)

请在CodeIgniter中尝试类似此查询的内容.. [虽然您的理想结果集中存在一些错误,但它会给您提示...]

SELECT t1.restaurant_id,t1.restaurant_name,t1.restaurant_code, SUM(total) FROM ( SELECT t1.restaurant_id,t1.restaurant_name,t1.restaurant_code, COUNT(t1.restaurant_id)as total FROM uhd_restaurant as t1 LEFT JOIN uhd_order_history as t3 ON t1.restaurant_id = t3.restaurant_id GROUP BY t1.restaurant_id UNION ALL SELECT t1.restaurant_id,t1.restaurant_name,t1.restaurant_code, COUNT(t1.restaurant_id)as total FROM uhd_restaurant as t1 LEFT JOIN uhd_user_order as t2 ON t1.restaurant_id = t2.restaurant_id GROUP BY t1.restaurant_id) as t1 GROUP BY t1.restaurant_id ORDER BY total DESC