foreign keys - MySQL join four tables and get some kind of SUM result -


i have 4 tables this:

mysql> describe courses; +-----------------+-------------+------+-----+---------+----------------+ | field           | type        | null | key | default |          | +-----------------+-------------+------+-----+---------+----------------+ | 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 |          | +-------------+--------------+------+-----+---------+----------------+ | 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 |          | +------------+--------------+------+-----+---------+----------------+ | 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 | | +-----------+---------+------+-----+---------+-------+ | course_id | int(11) | no   | pri | null    |       | | plan_id   | int(11) | no   | pri | null    |       | +-----------+---------+------+-----+---------+-------+ 

let me try explain have , do...

courses (course_id) has different steps (plan_id) wich has value of 1 or more days (plan_time).

course has 1 or more steps (course_to_plan)

a course connected pricegroup (price_id).

query mysql database , output off:
course_name, plan_id's has, , based on value of price_id value in plan_time result looks this:

+------------+--------------+------------+---------+ | course_name| pricegroup   | plan_time  | result  |  +------------+--------------+------------+---------+ | math       | expensive    | 7          | 3500    | +------------+--------------+------------+---------+ 

i hope understand me...
possible structure have or should "rebuild-and-redo-correct" something?

select c.course_name, p.price_name, sum(cp.plan_time), sum(cp.plan_time * p.price_value) 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 c.course_name, p.price_name 

please note seems me implementation might erroneous. way want data makes me think happier plan having price, don't apply same price plan "expensive" , plan "cheap", doing @ moment. don't know, intuitive :-)

thanks accepting answer, regards.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -