相当新的mysql,我此刻迷失了。这是我第一次在这里发帖。我的问题是在我介绍价格/回扣/佣金的历史表时进行查询。在我介绍这个表之前,我有以下查询来完成获取统计数据的工作:
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)
希望任何人都有这个问题的答案。 带着敬意 轰鸣
答案 0 :(得分:0)
如果您要在commodity_history表中添加一个时间戳,那么一切都会简单得多。 而不是changed_date,你需要一个from_date和一个to_date。 在创建新记录时(以及同时修改先前的commodity_history记录以设置其to_date时间戳),处理此操作非常简单。 搜索时间查询变得非常简单。
schgy2