我在每个表格中有3个包含restaurant_id
的表格,我试图在uhd_restaurant
加入uhd_user_order
和uhd_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_order
和uhd_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;
}
你可以帮助我如何获得正确的数据总量吗?
答案 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