2011-06-07 97 views
0

MySQL是相當新的,我迷失在此刻。這是我第一次在這裏發佈。我的問題是當我爲價格/回扣/佣金引入歷史表時進行查詢。之前我介紹了這個表,我有以下查詢doinmg獲得統計數據的工作:價格和產品查詢與歷史價格在MySQL分貝(3表)

select 
    cc_sale.cc_agent, 
    individual.surname as cc_agentname, 
    SUM(cc_sale.number_of) as sales, 
    commoditys.commodity_name,(commoditys.commission * SUM(cc_sale.number_of)) as commission_sum 
    FROM cc_sale 
    LEFT JOIN commoditys ON commoditys.id=cc_sale.commodity_id 
    LEFT JOIN destination ON destination.dest_id=cc_sale.cc_agent 
    LEFT JOIN individual ON individual.individual_id=destination.owner_id 
    WHERE cc_sale.project_id=$ccprojectid group by cc_sale.cc_agent,commoditys.commodity_name order by commission_sum desc; 

引進commodity_history表我很難找到一種方式去查詢得到同樣的統計數據後。在集

mysql> describe cc_sale; 
+--------------------+---------------------+------+-----+-------------------+----------------+ 
| Field    | Type    | Null | Key | Default   | Extra   | 
+--------------------+---------------------+------+-----+-------------------+----------------+ 
| id     | int(10) unsigned | NO | PRI | NULL    | auto_increment | 
| project_id   | int(10) unsigned | NO |  | NULL    |    | 
| cc_agent   | int(10) unsigned | NO |  | NULL    |    | 
| cc_called   | int(10) unsigned | NO |  | NULL    |    | 
| commodity_id  | int(10) unsigned | NO |  | NULL    |    | 
| number_of   | int(10) unsigned | NO |  | NULL    |    | 
| customer_number_of | int(10) unsigned | YES |  | NULL    |    | 
| status    | tinyint(3) unsigned | YES |  | 1     |    | 
| sale_date   | timestamp   | NO |  | CURRENT_TIMESTAMP |    | 
+--------------------+---------------------+------+-----+-------------------+----------------+ 

9行(0.00秒)

mysql> describe commoditys; 
+------------------+------------------+------+-----+---------------------+----------------+ 
| Field   | Type    | Null | Key | Default    | Extra   | 
+------------------+------------------+------+-----+---------------------+----------------+ 
| id    | int(10) unsigned | NO | PRI | NULL    | auto_increment | 
| commodity_id  | varchar(64)  | NO |  | NULL    |    | 
| commodity_name | varchar(64)  | NO |  | NULL    |    | 
| price   | decimal(10,2) | YES |  | NULL    |    | 
| kickback   | decimal(10,2) | YES |  | NULL    |    | 
| commission  | decimal(10,2) | YES |  | NULL    |    | 
| added_date  | timestamp  | NO |  | CURRENT_TIMESTAMP |    | 
| end_of_life_date | timestamp  | NO |  | 0000-00-00 00:00:00 |    | 
| cc_customer_id | int(10) unsigned | YES | MUL | NULL    |    | 
| project_id  | int(10) unsigned | NO |  | 0     |    | 
+------------------+------------------+------+-----+---------------------+----------------+ 

10行中集合(0.01秒)

mysql> describe commodity_history; 
+--------------+------------------+------+-----+-------------------+----------------+ 
| Field  | Type    | Null | Key | Default   | Extra   | 
+--------------+------------------+------+-----+-------------------+----------------+ 
| id   | int(10) unsigned | NO | PRI | NULL    | auto_increment | 
| c_id   | int(10) unsigned | NO |  | NULL    |    | 
| commodity_id | varchar(64)  | NO |  | NULL    |    | 
| price  | decimal(10,2) | YES |  | NULL    |    | 
| kickback  | decimal(10,2) | YES |  | NULL    |    | 
| commission | decimal(10,2) | YES |  | NULL    |    | 
| changed_date | timestamp  | NO |  | CURRENT_TIMESTAMP |    | 
+--------------+------------------+------+-----+-------------------+----------------+ 
7 rows in set (0.00 sec) 

從cc_sale:

+----+------------+----------+-----------+--------------+-----------+--------------------+--------+---------------------+ 
| id | project_id | cc_agent | cc_called | commodity_id | number_of | customer_number_of | status | sale_date   | 
+----+------------+----------+-----------+--------------+-----------+--------------------+--------+---------------------+ 
| 1 |   1 |  3 |  420 |   2 |   1 |    NULL |  2 | 2011-05-30 16:00:00 | 
| 2 |   1 |  3 |  421 |   2 |   2 |    NULL |  2 | 2011-05-30 16:00:00 | 
| 3 |   1 |  3 |  422 |   1 |   2 |    NULL |  2 | 2011-05-30 16:00:00 | 
| 77 |   1 |  3 |  804 |   1 |   2 |    NULL |  2 | 2011-06-06 15:00:00 | 
| 78 |   1 |  3 |  809 |   1 |   4 |    NULL |  2 | 2011-06-06 15:00:00 | 
| 79 |   1 |  3 |  823 |   1 |   2 |    NULL |  2 | 2011-06-06 15:00:00 | 
| 80 |   1 |  3 |  835 |   1 |   1 |    NULL |  2 | 2011-06-06 15:00:00 | 
| 81 |   1 |  3 |  835 |   2 |   1 |    NULL |  2 | 2011-06-06 15:00:00 | 
| 82 |   1 |  3 |  841 |   2 |   1 |    NULL |  2 | 2011-06-06 15:00:00 | 
| 83 |   1 |  3 |  852 |   3 |   1 |    NULL |  2 | 2011-06-06 15:00:00 | 
| 84 |   1 |  3 |  854 |   4 |   1 |    NULL |  2 | 2011-06-06 15:00:00 | 
| 85 |   1 |  3 |  862 |   4 |   1 |    NULL |  2 | 2011-06-06 15:00:00 | 
+----+------------+----------+-----------+--------------+-----------+--------------------+--------+---------------------+ 

85行中設置(0.00秒)

mysql> select * from commoditys; 
+----+--------------+----------------+---------+----------+------------+---------------------+---------------------+----------------+------------+ 
| id | commodity_id | commodity_name | price | kickback | commission | added_date   | end_of_life_date | cc_customer_id | project_id | 
+----+--------------+----------------+---------+----------+------------+---------------------+---------------------+----------------+------------+ 
| 1 | test1  | testeteste  | 100.00 | 40.00 |  15.00 | 2011-05-25 11:55:45 | 0000-00-00 00:00:00 |    1 |   1 | 
| 2 | test2  | telefonmøte | 0.00 | 1500.00 |  300.00 | 2011-05-25 13:17:39 | 0000-00-00 00:00:00 |    1 |   1 | 
| 3 | test3  | test3product | 300.00 | 150.00 |  50.00 | 2011-06-04 15:53:32 | 0000-00-00 00:00:00 |    1 |   1 | 
| 4 | test4  | test4product | 600.00 | 300.00 |  100.00 | 2011-06-04 15:55:25 | 0000-00-00 00:00:00 |    1 |   1 | 
| 5 | test5  | test5product | 1000.00 | 300.00 |  100.00 | 2011-06-04 23:24:40 | 2012-06-29 00:00:00 |    1 |   0 | 
+----+--------------+----------------+---------+----------+------------+---------------------+---------------------+----------------+------------+ 
5 rows in set (0.00 sec) 




mysql> select * from commodity_history; 
+----+------+--------------+---------+----------+------------+---------------------+ 
| id | c_id | commodity_id | price | kickback | commission | changed_date  | 
+----+------+--------------+---------+----------+------------+---------------------+ 
| 1 | 1 | test1  | 100.00 | 40.00 |  15.00 | 2011-05-25 11:55:45 | 
| 2 | 2 | test2  | 0.00 | 1500.00 |  300.00 | 2011-05-25 13:17:39 | 
| 3 | 3 | test3  | 300.00 | 150.00 |  50.00 | 2011-06-04 15:53:32 | 
| 4 | 4 | test4  | 600.00 | 300.00 |  100.00 | 2011-06-04 15:55:25 | 
| 5 | 5 | test5  | 1000.00 | 300.00 |  100.00 | 2011-06-04 23:24:40 | 
| 6 | 2 | test2  | 0.00 | 1000.00 |  200.00 | 2011-06-01 08:00:00 | 
| 7 | 1 | test1  | 200.00 | 150.00 |  100.00 | 2011-06-01 08:00:00 | 
+----+------+--------------+---------+----------+------------+---------------------+ 
7 rows in set (0.00 sec) 

每次將商品存放在商品中時,商品歷史中也會添加一行。 這樣我可能在此表中有幾行,但具有相同的commodity_id但changed_date不同。 我希望能夠查詢數據庫,並得到這樣的:

+----------+------+----------------+----------------+ 
| cc_agent |sales | commodity_name | commission_sum | 
+----------+------+----------------+----------------+ 
|  3 | 51 | telefonmøte |  15300.00 | ! These 2 are same commodity with 
|  3 | 5 | telefonmøte |  1000.00 | ! different price 
|  446 | 7 | telefonmøte |  2100.00 | 
|  448 | 2 | telefonmøte |   600.00 | 
|  3 | 40 | testeteste  |   600.00 | 
|  446 | 23 | testeteste  |   345.00 | 
|  3 | 2 | test4product |   200.00 | 
|  448 | 6 | testeteste  |   90.00 | 
|  3 | 1 | test3product |   50.00 | 
+----------+------+----------------+----------------+ 
8 rows in set (0.00 sec) 

希望有人能回答這個問題。 關於 Roar

回答

0

如果您將在commodity_history表中添加一個更多時間戳,那麼所有內容都會更加簡單。 改爲changed_date,您需要from_date和to_date。 在創建新記錄(以及同時修改以前的commodity_history記錄以將其設置爲to_date時間戳記)時處理此操作非常簡單。 搜索時間查詢變得非常簡單。

schgy2

+0

嗨,謝謝,當然,我可以添加另一個時間戳。但我不知道如何設置查詢來獲取提到的統計信息。任何提示? – rlorentz 2011-06-07 19:06:47

+0

做了改變,開始用新鮮的眼睛看着它。看起來它在歷史表中用end_date解決。非常感謝小費。 – rlorentz 2011-06-07 21:34:03