如何使用计算列来计算codeigniter中的另一列

时间:2016-06-28 18:28:04

标签: sql codeigniter nested-queries

我希望你能帮我解决这个简单的问题。 我尝试在codeigniter中使用这个嵌套查询。

Select
  ColumnA,
  ColumnB,
  calccolumn1,
  calccolumn1 / ColumnC as calccolumn2
From (
  Select
    ColumnA,
    ColumnB,
    ColumnC,
    ColumnA + ColumnB As calccolumn1
from testtable
);

如何将此转换为codeigniter? 我知道内部SELECT应该看起来像这样:

        $this->db->select('ColumnA,ColumnB,ColumnC,ColumnA + ColumnB As calccolumn1'); 
        $this->db->from('testtable');
        $subquery = $this->db->get();
        ...

但是我该如何继续?

2 个答案:

答案 0 :(得分:0)

当谈到更复杂的查询时,我通常会这样做:

   using(DbConnection conn = DBProvider.CreateConnection()){

            //Open the created connection
            conn.Open();

            //Create a new transaction
            using(DbTransaction tr = DBProvider.CreateTransaction()){

              //Begin a new transaction
              tr.Begin();

              bool saveOk;
              try{

                  //Updates customers by using dataadapter
                  dataAdapterCustomers.InsertCommand.Transaction = tr; 
                  dataAdapterCustomers.UpdateCommand.Transaction = tr;
                  dataAdapterCustomers.DeleteCommand.Transaction = tr;
                  dataAdapterCustomers.Update();

                  //Updates stock items by using dataadapter
                  stockAdapterCustomers.InsertCommand.Transaction = tr; 
                  stockAdapterCustomers.UpdateCommand.Transaction = tr;
                  stockAdapterCustomers.DeleteCommand.Transaction = tr;
                  stockAdapterCustomers.Update();  

                  //...Many other DB accessing here... 

                  //Updates stock quantity by using simple DBCommand
                  quantityUpdateCmd.Transaction = tr;
                  quantityUpdateCmd.ExecuteNonQuery();

                  //Updates stock statistics by using a simple DBCommand
                  updateStockStatsCmd.Transaction = tr;
                  updateStockStatsCmd.ExecuteNonQuery();


                  //...Many other DB accessing here... 

                  //HERE:
                  //Creates a new activity and save it using EF.
                  //I use a UnitOfWork and i pass to it my connection and 'false' as contextOwnsConnection parameter 
                  //(it 'll be used by the DBContext contained in my Unit of work)
                  using(ActivityUoW uow = new ActivityUoW(conn, false)){

                     Activity act = new Activity();
                     act.Name = "Saving activity";
                     act.Description = "Done by user";
                     act.Date = DateTime.Now;

                     uow.Activities.Add(act);
                     uow.SaveChanges();
                   }


                  //Based on activity result, launch a store procedure that makes other complex things. 
UNFORTUNATELY THE CONNECTION HAS BEEN CLOSED AND TRANSACTION COMMITTED, SO THE FOLLOWING INSTRUCTION WILL FAIL.

                  launchActivityUpdateSpCmd.Transaction = tr;
                  launchActivityUpdateSpCmd.ExecuteNonQuery();


                   //...Many other DB accessing here... 

                   //Data saved correctly  
                   saveOk = true;

               }
               catch(Exception ex){

                  //There was an error during save
                  saveOk = false;
               }


               //Commit or rollback transaction according to save procedure result 
               if(saveOk) 
                 tr.Commit();
               else
                 tr.Rollback();
            }
        }

答案 1 :(得分:0)

谢谢你的快速回复! 查询工作正常!但我似乎无法找到如何从我想要添加的方法添加我的变量($ data)的WHERE子句。 通常我会用

$ this-> db-> where(' Username',$ data);

但由于where子句现在位于查询中,我还没有找到将其添加到查询中的方法。