2010-03-31 111 views
0

說,我得到了這兩個表....有沒有辦法來優化這個mysql查詢...?

Table 1 : Hotels 
    hotel_id hotel_name 
     1   abc 
     2   xyz 
     3   efg 

Table 2 : Payments 
    payment_id  payment_date  hotel_id  total_amt comission 
     p1   23-03-2010   1   100   10 
     p2   23-03-2010   2   50   5 
     p3   23-03-2010   2   200   25 
     p4   23-03-2010   1   40   2 

現在,我需要從兩個表

  1. 給定一個特定日期(比如得到以下細節,23-03- 2010年),在該特定日期支付的每家酒店的總金額總和。
  2. 所有這些都23-03-2010根據酒店名稱

示例輸出如下下令日期的行...

+------------+------------+------------+---------------+ 
| hotel_name | date  | total_amt | commission | 
+------------+------------+------------+---------------+ 
| * abc  | 23-03-2010 |  140 |  12  | 
+------------+------------+------------+---------------+ 
|+-----------+------------+------------+--------------+| 
|| paymt_id | date  | total_amt | commission || 
|+-----------+------------+------------+--------------+| 
|| p1  | 23-03-2010 |  100 |  10  || 
|+-----------+------------+------------+--------------+| 
|| p4  | 23-03-2010 |  40  |  2  || 
|+-----------+------------+------------+--------------+| 
+------------+------------+------------+---------------+ 
| * xyz  | 23-03-2010 |  250 |  30  | 
+------------+------------+------------+---------------+ 
|+-----------+------------+------------+--------------+| 
|| paymt_id | date  | total_amt | commission || 
|+-----------+------------+------------+--------------+| 
|| p2  | 23-03-2010 |  50  |  5  || 
|+-----------+------------+------------+--------------+| 
|| p3  | 23-03-2010 |  200 |  25  || 
|+-----------+------------+------------+--------------+| 
+------------------------------------------------------+ 

表的樣本上面說已要打印...

的想法是第一個顯示每家酒店的合併細節,而當「*」旁邊點擊的付款細節擊穿將變得可見酒店的名字......但這可以通過一些jquery完成.. !!!表本身可以用php生成...

現在我使用了兩個單獨的查詢:一個獲得金額和佣金按酒店名稱分組的總和。接下來是獲取表中具有該日期的每個條目的單獨行。這是當然的,因爲它把記錄計算總和()返回每個酒店與金額的總和只有一排...

有沒有辦法將這兩個查詢組合成一個單一的一個並以更優化的方式進行操作......?

希望我是清楚..感謝您的時間和答覆...

編輯:添加查詢過

  1. 查詢得到的總和()

    
    SELECT DATE_FORMAT($payments.payment_date, '%d-%m-%Y %T') as payment_date, 
         $payments.payment_id AS payment_id, 
         $payments.payment_amount AS payment_amount, 
         $payments.agent_commision AS commision, 
         $payments.comm_percentage AS percentage, 
         $hotels.hotel_name AS hotel 
         SUM($payments.payment_amount) AS tot_amt 
         SUM($payments.agent_commision) AS tot_ag_comsn 
    FROM $payments 
    JOIN $hotels 
    ON $payments.hotel_id = $hotels.hotel_id 
    WHERE DATE_FORMAT(payment_date,'%d-%m-%Y') = '$date_report' 
    GROUP BY $payments.hotel_id 
    ORDER BY $payments.payment_date ASC 
    

  2. 查詢以獲取單個行

    
    SELECT DATE_FORMAT($payments.payment_date, '%d-%m-%Y %T') as payment_date, 
          $payments.payment_id AS payment_id, 
          $payments.payment_amount AS payment_amount, 
          $payments.agent_commision AS commision, 
          $payments.comm_percentage AS percentage, 
          $hotels.hotel_name AS hotel 
    FROM $payments 
    JOIN $hotels 
    ON $payments.hotel_id = $hotels.hotel_id 
    WHERE DATE_FORMAT(payment_date,'%d-%m-%Y') = '$date_report' 
    GROUP BY $payments.payment_date 
    ORDER BY $payments.payment_date ASC 
    

    我加入由兩個查詢返回的行中的兩個獨立的臨時表,然後使用PHP來打印表,如上圖所示...

編輯2:並且也明白,如果有人提出了一種這個崗位更好的標題...;)

+0

發佈查詢? – Xorlev 2010-03-31 06:42:38

+0

@xorlev:編輯後添加查詢過...謝謝... – SpikETidE 2010-03-31 06:44:32

回答

0

這是報告的作家在編程語言的域名,或等效邏輯。

+0

是...打印表格與結構,當然,邏輯的編程語言的域名。我只是想知道如果應該在表中打印的細節可以通過單個查詢來檢索.... – SpikETidE 2010-03-31 06:42:22

相關問題