【问题标题】:MySQL join four tables and get some kind of SUM resultMySQL 连接四个表并获得某种 SUM 结果
【发布时间】:2012-06-04 19:53:52
【问题描述】:

我有四个这样的表:

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 数据库并获得输出:
course_nameplan_id 的值,并根据 price_id 的值以及 plan_time 中的值得到如下所示的结果:

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

希望你能理解我...
我拥有的结构是否有可能,或者我应该“重建并重做正确”的东西?

【问题讨论】:

  • RESULT 是如何计算的?是price_value * plan_time 吗?
  • 结果 = price_value * plan_time?
  • 例如:课程 X 有三个计划,其值为 10,5,15,它将使用课程连接到的 price_id 计算.例如,这可以具有值500。我的课程 X 的结果将是 30 * 500 = 15000

标签: mysql foreign-keys relationship


【解决方案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

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

感谢您接受答案,问候。

【讨论】:

  • 谢谢!工作就像一种魅力
【解决方案2】:

让我看看我是否明白你需要什么:

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

【讨论】:

  • 老实说,这就是我说“幸运一击”的原因,这只是运气.. 它也可以是 COUNT 没有任何额外的细节:-)
  • 你们俩都很棒 :) 非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-04
  • 1970-01-01
  • 2013-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多