我是Codeigniter的新手:如何将此SQL转换为Codeigniter模型

时间:2019-10-04 08:09:20

标签: sql codeigniter

SELECT  stock_Code AS stockCode
                            , items.item_Code
                    , item_Name AS itemName
                    , item_units.name AS unitName
                    , item_Cost AS itemCost
                    , stock_Quantity AS stockQuantity
                    , supplier.supplier_Name as supplierName
            FROM items
            LEFT JOIN stocks  ON items.item_Code = stocks.item_Code
            LEFT JOIN supplier ON supplier.supplier_Code = items.supplier_Code
            LEFT JOIN item_units ON items.unit_id = item_units.unit_id
            WHERE stocks.created_at = (select max(stocks.created_at)
                                        FROM stocks
                                        WHERE stocks.item_Code = items.item_Code
                                        ) AND items.item_Code = '123'

4 个答案:

答案 0 :(得分:1)

我只是想提供帮助。

查询:

hash = { points: [] }

abc = transform_values(@:dup)
hash[:points] << 1 << 2

hash #=> {:points=>[1, 2]} 
abc  #=> {:points=>[]} 

如果需要使用数组对象获取单行数据:

$this->db->select("SELECT  stock_Code AS stockCode
                        , items.item_Code
                , item_Name AS itemName
                , item_units.name AS unitName
                , item_Cost AS itemCost
                , stock_Quantity AS stockQuantity
                , supplier.supplier_Name as supplierName");
$this->db->from("items");
$this->db->join("stocks", "items.item_Code = stocks.item_Code", "left");
$this->db->join("supplier","supplier.supplier_Code = items.supplier_Code", "left");
$this->db->join("item_units","items.unit_id = item_units.unit_id","left");
$this->db->where("stocks.created_at = (select max(stocks.created_at) 
");

如果您需要使用数组获取单行数据:

$this->db->get()->row();

如果您需要获取多行数据数组对象:

$this->db->get()->row_array();

如果您需要获取多行数据数组:

$this->db->get()->result();

答案 1 :(得分:0)

尝试一下:-

$sql = $this->db->query("SELECT  stock_Code AS stockCode
                            , items.item_Code
                    , item_Name AS itemName
                    , item_units.name AS unitName
                    , item_Cost AS itemCost
                    , stock_Quantity AS stockQuantity
                    , supplier.supplier_Name as supplierName
            FROM items
            LEFT JOIN stocks  ON items.item_Code = stocks.item_Code
            LEFT JOIN supplier ON supplier.supplier_Code = items.supplier_Code
            LEFT JOIN item_units ON items.unit_id = item_units.unit_id
            WHERE stocks.created_at = (select max(stocks.created_at)
                                        FROM stocks
                                        WHERE stocks.item_Code = items.item_Code
                                        ) AND items.item_Code = '123'");
        $sql->result();
        $row = $sql->row_array();

答案 2 :(得分:0)

如果要使用codeigniter模型,则 1.在您的模型文件夹中创建一个模型(请参见codeigniter指南模型) 2.在模型中以

编写函数
$sql = "Your query ";
$result = $this->db->query($sql);

所以您得到结果并返回。 3.在控制器中,加载模型类并调用您创建的函数以获得结果。

但是,如果要将查询转换为活动记录,这是另一个问题。

答案 3 :(得分:0)

public function aaa(){

  $this->db->select("SELECT stock_Code AS stockCode, 
                          items.item_Code, 
                          item_Name AS itemName, 
                          item_units.name AS unitName, 
                          item_Cost AS itemCost, 
                          stock_Quantity AS stockQuantity, 
                          supplier.supplier_Name as supplierName")
           ->from("items")
           ->join("stocks", "items.item_Code = stocks.item_Code")
           ->join("supplier","supplier.supplier_Code = items.supplier_Code")
           ->join("item_units","items.unit_id = item_units.unit_id")
           ->where("stocks.created_at = (select max(stocks.created_at) 
       ");
}