1

我有四个这样的表:

mysql> describe courses;
+-----------------+-------------+------+-----+---------+----------------+
| Field           | Type        | Null | Key | Default | Extra          |
+-----------------+-------------+------+-----+---------+----------------+
| course_id       | int(11)     | NO   | PRI | NULL    | auto_increment |
| course_name     | varchar(75) | YES  |     | NULL    |                |
| course_price_id | int(11)     | YES  | MUL | NULL    |                |
+-----------------+-------------+------+-----+---------+----------------+

mysql> describe pricegroups;
+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| price_id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| price_name  | varchar(255) | YES  |     | NULL    |                |
| price_value | int(11)      | YES  |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+

mysql> describe courseplans;
+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| plan_id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| plan_name  | varchar(255) | YES  |     | NULL    |                |
| plan_time  | int(11)      | YES  |     | NULL    |                |
+------------+--------------+------+-----+---------+----------------+

mysql> describe course_to_plan;
+-----------+---------+------+-----+---------+-------+
| Field     | Type    | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| course_id | int(11) | NO   | PRI | NULL    |       |
| plan_id   | int(11) | NO   | PRI | NULL    |       |
+-----------+---------+------+-----+---------+-------+

让我试着解释一下我有什么以及我想做什么......

我所有的课程(course_id)都有不同的步骤(plan_id),其值为 1 天或更多天(plan_time)。

课程有一个或多个步骤 ( course_to_plan)

课程连接到价格组 ( price_id)。

我想查询我的 MySQL 数据库并得到一个输出:
The course_name, the plan_id's it has, and based on the value of the value and price_idthe value in the plan_timeget a result who looks like this:

+------------+--------------+------------+---------+
| course_name| pricegroup   | plan_time  | RESULT  | 
+------------+--------------+------------+---------+
| Math       | Expensive    | 7          | 3500    |
+------------+--------------+------------+---------+

我希望你能理解我......
我拥有的结构是否有可能,或者我应该“重建和重做”什么?

4

2 回答 2

1

让我看看我是否了解您的需求:

SELECT c.course_name, pg.price_name, 
       COUNT(cp.plan_time), SUM(pg.price_value * cp.plan_time) AS result
FROM courses c 
INNER JOIN pricegroups pg ON c.course_price_id = pg.price_id
INNER JOIN course_to_plan ctp ON c.course_id = ctp.course_id
INNER JOIN courseplans cp ON ctp.plan_id = cp.plan_id
GROUP BY c.couse_name, pg.price_name
于 2012-06-04T20:02:37.280 回答
1
SELECT c.course_name, p.price_name, SUM(cp.plan_time), SUM(cp.plan_time * p.price_value)
FROM courses c
    INNER JOIN pricegroups p        ON p.price_id = c.course_price_id
    INNER JOIN course_to_plan cpl   ON cpl.course_id = c.course_id
    INNER JOIN courseplans cp       ON cp.plan_id = cpl.plan_id
GROUP BY c.course_name, p.price_name

请注意,在我看来,您的实施可能是错误的。您想要数据的方式使我认为您可能会对有价格的计划感到满意,因此您不会对“昂贵”的计划和另一个“便宜”的计划应用相同的价格,这就是你现在正在做。但我真的不知道,这很直观:-)

感谢您接受答案,问候。

于 2012-06-04T20:06:11.210 回答