MySQL连接四个表并获得某种SUM结果

标签 mysql foreign-keys relationship

我有四个这样的表:

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    |       |
+-----------+---------+------+-----+---------+-------+

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

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

一门类(class)有一个或多个步骤 ( course_to_plan )

一门类(class)连接到一个价格组 ( price_id )。

我想查询我的 MySQL 数据库并关闭输出:
course_name , plan_id它有,并且基于 price_id 的值连同 plan_time 中的值得到一个看起来像这样的结果:

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

我希望你能理解我...
我有没有可能使用我的结构或者我应该“重建并重做”一些东西?

最佳答案

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

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

感谢您接受答案,问候。

关于MySQL连接四个表并获得某种SUM结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10887431/

相关文章:

php - 从MySQL中选择数据进行多折线图

foreign-keys - TypeORM 关系 : Only IDs instead of whole instances

mysql - 为 n :m relationship 创建表

mysql导入具有巨大blob表字段的大文件

java jdbc mysql 连接器 : how to resolve disconnection after a long idle time

sql - 外键引用多个表

mysql - 建表时实体一一对应

laravel - Eloquent 查询 : All News with same categories. 数据透视表

php - 使用nodejs+socket.io和mysql聊天

mysql - 从外部表中选择值