2016-02-16 77 views
0

我將加入4個表以獲取第一個表中存在的金額的總和。到目前爲止,我已經嘗試了很多方法來直接獲得總和,但是如果我不使用group_by,它會變成0(如果我是group_by),或者是重複的。這裏是我的代碼:Codeigniter SUM和LEFT JOIN刪除重複項

$this->db->select('COALESCE(SUM(cust_ reservation_request.dollar_amount), "0") as total'); 
     $this->db->join('cust_ reservation_request', 'cust_ reservation_request.id = cust_reservations.rid', 'LEFT'); 
     $this->db->join('attendees', 'cust_rid= cust_ reservation_request.id', 'LEFT'); 
     $this->db->join('cust_reservation_cancelled', 'cust_reservation_cancelled.rid = cust_ reservation_request.id', 'LEFT'); 
     $this->db->where('cust_ reservation_request.tid IN (5, 6, 7, 8)->where('attendees.status', 1)->where('cust_reservation_cancelled.rid IS NULL'); 
     $this->db->group_by('cust_reservation_request.id'); 
     $total = $this->db->get('cust_reservations')->row()->total; 

我得到0爲$總變量的值,而應該是288 然而,當我刪除$this->db->group_by('cust_reservation_request.id');聲明,我得到重複上attendees.attendees.cust_rid,這實際上是在那裏, $ total變爲498,因爲可能有多個參與者爲同一個擺脫。我想刪除這個重複的,保留所有dollar_amount字段的實際SUM。

我的表是:

cust_ reservation_request 
id // index, foreign key in rest of other tables 
tid 
uid 
dollar_amount 

cust_reservations 
id 
rid // related to id in cust_ reservation_request table 
reservation_date 
pay_token 

cust_ reservation_cancelled 
id 
rid // related to cust_ reservation_request.id 
cancel_date 

attendees 
id 
cust_rid // its the same rid, related to cust_reservation_request.id 
status 

你認爲有可能做到這一點任何安全的方式,刪除重複,同時以適當的方式做SUM?

非常感謝您的幫助。

+0

嘗試運行它沒有「WHERE cust_reservation_cancelled.rid IS NULL」條件 – IgorM

+0

你可以運行它作爲服務器上的SQL查詢而不處理框架?這樣可以更容易地調試 – IgorM

+0

Hi @IgorM感謝您的回覆。我已經試過它在服務器上運行,它在我添加group_by時返回0,並且在使用SUM之後返回重複的行。我也嘗試使用SELECT *而不是SUM來查看返回的結果,並且在那裏我知道它會返回一行兩次。 – Zafar

回答

0

我原來的回答是:「我認爲你的數據有問題,並且查詢返回NULL被COALESCE轉換爲0」,但這不是問題。 「參加者」表中的「狀態」字段出現問題,主要由主題啓動器解決,所以我不配點

Zafar,很高興您設法解決問題。來自以色列:)

+0

正如我已經解釋過的,我試圖在沒有'COALESCE'的情況下運行它,只需將它放入select:'SELECT SUM(user_reservation_request.dollar_amount)作爲total'並且它不起作用。它再次給了我0。 – Zafar

+0

如果我的數據有問題,當我刪除group_by時,它不應該返回一定數量!它返回一些金額,但有一個重複的條目,我可以看到它,當我在mySQL中運行它。 – Zafar

+0

@Zafar,你說你得到0運行下面的查詢SELECT SUM(dollar_amount)FROM cust_ reservation_request ...你不覺得你的數據類型有什麼問題嗎? – IgorM

0

問候它可能是這樣的...... (翻譯的怎麼會是在大型機)

它會是這樣的:

$this->db->select('COALESCE(SUM(cust_ reservation_request.dollar_amount), "0") as total'); 
      $this->db->where exists (select 1 from cust_reservations where cust_reservations.rid = cust_ reservation_request.id); 
      $this->db->and exists (select 1 from attendees where cust.rid = cust_reservation_request.id); 
      $this->db->and exists (select 1 from cust_ reservation_cancelled where cust_reservation_cancelled.rid = cust_reservation_request.id); 
      $this->db->and ('cust_ reservation_request.tid IN (5, 6, 7, 8)->where('attendees.status', 1)->where('cust_reservation_cancelled.rid IS NULL'); 
      $this->db->group_by('cust_reservation_request.id'); 
      $total = $this->db->get('cust_reservations')->row()->total; 

你必須檢查語法和所有的()和',因爲我不知道除普通SQL以外的語言,您可能必須在'存在'處更改'並存在',和/或稍後將其更改爲和。

此查詢的目的(在普通SQL中)是,您只想從一個表中總結dollar_amount(cust_ reservation_request),您希望確保在其他表中存在具有相同row_id的行。

希望這會有所幫助。

+0

感謝您的評論@BenLink,但是,正如IgorM ,我用子查詢解決了它,然後通過全局應用'where attendees.status = 1'子句。不過,我感謝你的努力。謝謝。 – Zafar