我有四個表是這樣的:MySQL的加入四個表,並得到某種SUM的結果
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_name
,該plan_id
的它,以及基於price_id
與在plan_time
值在一起的價值得到的結果誰看起來像這樣的:
+------------+--------------+------------+---------+
| course_name| pricegroup | plan_time | RESULT |
+------------+--------------+------------+---------+
| Math | Expensive | 7 | 3500 |
+------------+--------------+------------+---------+
我希望你明白我...
它甚至有可能與我或者我應該「重建-和重做,正確」的東西結構?
如何計算RESULT?它是'price_value * plan_time'嗎? – eggyal
result = price_value * plan_time? – Sebas
例如:課程X有三個計劃,其值爲'10','5','15',並且將使用該課程所連接的'price_id'進行計算。這可以例如具有值「500」。我的課程X的結果將是'30 * 500 = 15000' – David