每周,我的客户都会通过第三方营销小组开展广播节目和电子邮件活动,以推广他的产品。他想知道他每周赚/花多少钱,并为他分解数据。表数据如下所示:
tablename: orders
+----------+--------------+---------------------+------------+
| order_id | order_amount | referral | order_date |
+----------+--------------+---------------------+------------+
| 1 | 16.00 | Radio: Jack Johnson | 2013-07-01 |
| 2 | 50.00 | Radio: Jack Johnson | 2013-07-02 |
| 3 | 16.00 | Radio: Jack Johnson | 2013-07-02 |
| 4 | 50.00 | Radio: Thabo Mbeki | 2013-07-03 |
| 5 | 16.00 | Email: Renewal | 2013-07-08 |
| 6 | 50.00 | Other | 2013-07-08 |
| 7 | 16.00 | Email: Renewal | 2013-07-08 |
| 8 | 50.00 | Radio: Thabo Mbeki | 2013-07-09 |
+----------+--------------+--------------+-------------------+
tablename: media
+----------+-------+-------+--------------+-------+------------+
| media_id | spots | cost | name | type | date |
+----------+-------+-------+--------------+-------+------------+
| 1 | 10 | 10000 | Jack Johnson | radio | 2013-07-01 |
| 2 | 5 | 5000 | Thabo Mbeki | radio | 2013-07-01 |
| 3 | 10 | 10000 | Jack Johnson | radio | 2013-07-08 |
| 4 | 5 | 5000 | Thabo Mbeki | radio | 2013-07-08 |
| 5 | 1 | 250 | Renewal | email | 2013-07-08 |
+----------+---------------+--------------+--------------------+
我需要生成一个这样的 HTML 表:
+------------+--------------+-------+------+----------+---------+--------------+----------------+
| week | name | type |spots | cost | revenue | total orders | avg. per order |
+------------+--------------+-------+------+----------+---------+--------------+----------------+
| 2013-07-01 | Jack Johnson | radio | 10 | 10000 | 82.00 | 3 | 27.33 |
| 2013-07-01 | Thabo Mbeki | radio | 5 | 5000 | 50.00 | 1 | 50.00 |
+------------+--------------+-------+------+----------+---------+--------------+----------------+
我试图弄清楚如何加入表格并按订单中的推荐字段对它们进行分组对我来说变得很复杂。由于 orders.referral 与 media.name 不匹配,我猜有一个WHERE referral LIKE %name
但我不知道该怎么做。