【问题标题】:How to use join properply in codeigniter with multiple tables如何在具有多个表的codeigniter中正确使用join
【发布时间】:2019-05-28 11:00:55
【问题描述】:

我正在尝试在 codeigniter 中连接 5 个表,但在获得正确结果时遇到了一些麻烦,请帮忙。我认为错误在我的查询中,但我不确定。

我已经尝试了很多 join 语句,但我似乎找不到正确的答案。这就是为什么我在这里发布它,因为我已经绝望了,请帮助某人〜

这里是架构和数据库。希望这会有所帮助

架构

数据库

 public function get_receiving_details_mawb($id){

        $this->ops_db->select('d.quantity, e.sku_code, a.quantity AS actual, 
        abs(d.quantity - a.quantity) AS variance');
        $this->ops_db->from('sk_rcv_d a');
        $this->ops_db->where_in('a.rcv_id', $id);
        $this->ops_db->join('sk_rcv_h b', 'a.rcv_id = b.id', 'inner');
        $this->ops_db->join('sk_booking_h c', 'b.booking_id = c.id','inner');
        $this->ops_db->join('sk_booking_d d', 'c.id = d.book_id', 'inner');
        $this->ops_db->join('sk_item e', 'd.item_id = e.id', 'inner');
        $this->ops_db->group_by('d.id');//d.id
        $query = $this->ops_db->get();
        return $query->result_array();
    }

    public function get_rcvh_mawb($id) {

       $this->ops_db->where("mawb_no", $id);
       $query = $this->ops_db->get("sk_rcv_h");
       $how = array();
       foreach($query->result_array() as $row) {
         $id = $row['id'];
         $how[] = $id;
       }
       return $how; 
    }

-------------------------------
        $wat  = $this->wh_db->get_rcvh_mawb($mawb);
        $item_details = array();
        $items = array();
        $results  = $this->wh_db->get_receiving_details_mawb($wat);
        foreach($results as $res) {
          extract($res);
          if (! array_key_exists($sku_code, $items)) {
          $items[$sku_code] = array("sku_code" => $sku_code, "quantity" => 0, 
          "actual" => 0, "variance" => 0);
         }
         $items[$sku_code]['quantity'] = $quantity;
         $items[$sku_code]['actual'] += $actual;
         $items[$sku_code]['variance'] = $items[$sku_code]['quantity'] - 
         $items[$sku_code]['actual']; 
        }

        foreach($items as $res) {
        $item_details[] = array("sku" => $res['sku_code'], "qty" => 
        $res['quantity'], "actual" => $res['actual'], "variance" => 
        $res['variance']);
        }

        $final     = array("mawb_no" => $mawb, "status" => $stat_api, 
        "status_date" => $stat_api_date, "items" => $item_details);

这是我得到的结果:

     {
        "mawb_no": "MASTERPOD-1",
        "status": "Received",
        "status_date": "2019-05-27 19:35:42",
        "items": [
            {
                "sku": "RTISSUE",
                "qty": "50",
                "actual": 50,
                "variance": 0
            },
            {
                "sku": "ISOALCO",
                "qty": "102",
                "actual": 50,
                "variance": 52
            }
        ]
    }

我应该得到什么:

    {
        "mawb_no": "MASTERPOD-1",
        "status": "Received",
        "status_date": "2019-05-27 19:35:42",
        "items": [
            {
                "sku": "RTISSUE",
                "qty": "50",
                "actual": 50,
                "variance": 0
            },
            {
                "sku": "ISOALCO",
                "qty": "102",
                "actual": 102,
                "variance": 0
            }
        ]
     }

【问题讨论】:

  • 感觉你应该在查询中使用SUM(a.quantity) AS actual。休息一切似乎都很好。从 php sn-p 中删除添加并检查。
  • 得到了同样的结果 :(
  • 我的组正确吗?
  • 如果可以的话,将带有一些数据的架构导出并发送给我,这样我会更快地提出与您想要的结果相匹配的正确查询,而不是尝试研究上述关系。
  • @SherifSalah 我如何在评论中添加一个 zip 文件对不起

标签: php sql codeigniter join


【解决方案1】:

根据您的方案,它一切正常,但通过内部连接,它会根据您示例中的条件为您提供所有四种可能的结果,您需要在获得结果时准确指定您想要的结果,您需要一个条件,通过添加having 条件,我能够模仿您想要的结果,我不确定它是否在所有情况下都有效,但您可以尝试并且无论如何您都会得到这个想法(条件输入或条件结果):

$this->db->select('d.quantity, e.sku_code, a.quantity AS actual, abs(d.quantity - a.quantity) AS variance');
$this->db->from('sk_rcv_d a');
$this->db->where('a.rcv_id', $id);
$this->db->join('sk_rcv_h b', 'a.rcv_id = b.id', 'inner');
$this->db->join('sk_booking_h c', 'b.booking_id = c.id', 'inner');
$this->db->join('sk_booking_d d', 'c.id = d.book_id', 'inner');
$this->db->join('sk_item e', 'd.item_id = e.id', 'inner');
$this->db->having('actual = quantity');

在这种情况下你不需要groub_by,上面的查询会给你这个:

array(2) {
  [0] => array(4) {
    ["quantity"] => string(2) "50"
    ["sku_code"] => string(7) "RTISSUE"
    ["actual"] => string(2) "50"
    ["variance"] => string(1) "0"
  }
  [1] => array(4) {
    ["quantity"] => string(3) "102"
    ["sku_code"] => string(7) "ISOALCO"
    ["actual"] => string(3) "102"
    ["variance"] => string(1) "0"
  }
} 

【讨论】:

  • @noob_girl .. 花了大约一个小时才弄明白哈哈哈.. 很高兴它成功了,随时欢迎您^-^
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-11
  • 2021-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-23
相关资源
最近更新 更多