有没有办法优化这个mysql查询...?

时间:2010-03-31 06:33:48

标签: mysql query-optimization

说,我有两张桌子......

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),在该特定日期已支付的每家酒店的total_amt总和。
  2. 根据酒店名称
  3. 订购日期为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 ...

    生成

    现在我正在使用两个单独的查询:一个用于获取按酒店名称分组的金额和佣金的总和。接下来是获取表中具有该日期的每个条目的单独行。当然,这是因为对计算sum()的记录进行分组只会为每个酒店返回一行,并且金额总和...

    有没有办法将这两个查询合并为一个,并以更优化的方式进行操作...... ??

    希望我很清楚..感谢您的时间和回复...

    编辑:添加查询

    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打印如上所示的表...

    3. 编辑2:而且,如果有人为这篇文章提出了更好的标题,那么也要感谢...;)

1 个答案:

答案 0 :(得分:0)

这是报告编写者的领域,或编程语言中的等效逻辑。