如何在Codeigniter中合并2个查询

时间:2019-02-13 12:14:00

标签: php mysql sql codeigniter

我有2个查询。我想合并两个查询。当试图添加两个查询都没有得到结果时。 请在下面找到2个查询。甚至我试图在codeigniter模型中添加一个单一的核心PHP查询 但没有得到结果。谁能帮我解决这个问题。

模型codeigniter-method1中的基于PHP的核心查询

$sql = "select
                'Amadeus' AS SupplierName,TST_Type,TKT_Type,TKTNumber,TKT_FO_REF,BasePrice,TaxPrice,Commission1,BasePrice+TaxPrice AS TotalPrice,'issued' AS tkt_type,booking_price.CreatedBy as createdby,`TicketedDate` AS created_date,CONCAT(`a3m`.`firstname`,`a3m`.`lastname`) AS created_name,a3m.account_id,`a3m`.locationvalue AS Location_ID,`a3m`.locationdescription AS Location_Name,`a3m`.costcentervalue AS CostCenter_ID,`a3m`.costcenterdescription AS CostCenter_Name
                 from 
                 data_flight.booking_price
                 left join `a3m_account_details` `a3m` ON `a3m`.`account_id` = data_flight.booking_price.CreatedBy  
                 where TKTNumber!=''

                 union all

                 select 
                 'Amadeus' AS SupplierName,'' AS TST_Type,'' AS  TKT_Type,br_tkt AS TKTNumber,'' AS TKT_FO_REF,br_fare_paid_B AS BasePrice, br_tax_refund_TXT AS  TaxPrice,
                 br_cancellation_penalty AS Commission1, br_fare_total_RFT AS TotalPrice,br_cancel_type AS tkt_type,booking_refund.br_added_by AS createdby,br_refund_date AS created_date      ,CONCAT(`a3m`.`firstname`,`a3m`.`lastname`) AS created_name,a3m.account_id,`a3m`.locationvalue AS Location_ID,`a3m`.locationdescription AS Location_Name,`a3m`.costcentervalue AS CostCenter_ID,`a3m`.costcenterdescription AS CostCenter_Name
                from  
                 data_flight.booking_refund  
                left join `a3m_account_details` `a3m` ON `a3m`.`account_id` = data_flight.booking_refund.br_added_by
                where br_tkt!=''
                ";

        echo $this->db->query($sql);

基于COdeigniter的查询方法2

$this->db->select('"Amadeus" AS SupplierName,TST_Type,TKT_Type,TKTNumber,TKT_FO_REF,BasePrice,TaxPrice,Commission1,(BasePrice+TaxPrice) AS TotalPrice,"issued" AS tkt_type,createdby,TicketedDate as created_date,CONCAT(a3m.firstname," ",a3m.lastname) AS created_name,a3m.account_id,a3m.locationvalue AS Location_ID,a3m.locationdescription AS Location_Name,a3m.costcentervalue AS CostCenter_ID,a3m.costcenterdescription AS CostCenter_Name');
        $this->db->from($this->traveldb->database.".booking_price bp");
        $this->db->join('a3m_account_details a3m','a3m.account_id=createdby','left');
        $this->db->where('TKTNumber !=', '');
        $query1 = $this->db->get(); 

         $this->db->select('"Amadeus" AS SupplierName,"" AS TST_Type,"" AS  TKT_Type,br_tkt AS TKTNumber,"" as TKT_FO_REF,br_fare_paid_B as BasePrice,br_tax_refund_TXT as TaxPrice,br_cancellation_penalty as Commission1,br_fare_total_RFT AS TotalPrice,br_cancel_type AS tkt_type,br_added_by as createdby,br_refund_date as created_date,CONCAT(a3m.firstname," ",a3m.lastname) AS created_name,a3m.account_id,a3m.locationvalue AS Location_ID,a3m.locationdescription AS Location_Name,a3m.costcentervalue AS CostCenter_ID,a3m.costcenterdescription AS CostCenter_Name');
        $this->db->from($this->traveldb->database.".booking_refund bp");
        $this->db->join('a3m_account_details a3m','a3m.account_id=br_added_by','left');
        $this->db->where('br_tkt !=', '');
        $query2 = $this->db->get(); 

        $query = $this->db->query($query1." UNION ".$query2); 

     echo $this->db->last_query();

我尝试了模型中的两种方法均未获得结果,尝试打印查询以显示错误。请帮助我。

0 个答案:

没有答案