Hi all, I have what I hope is a simple question. I'm kind of new to working with joins so this is probably an easy question..
I have a database structure set up for an online shop. I have a products table, which has an id and several fields of data:
Code:
mysql> describe products;
+-------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| title | varchar(255) | YES | | NULL | |
| description | text | YES | | NULL | |
| short_description | text | YES | | NULL | |
| type_id | int(10) unsigned | NO | | NULL | |
| price | decimal(10,2) | YES | | 0.00 | |
| active | tinyint(1) | NO | | 1 | |
| image | varchar(255) | YES | | NULL | |
| bulk_orders | tinyint(1) | NO | | 0 | |
| featured | tinyint(1) | YES | | 0 | |
+-------------------+------------------+------+-----+---------+----------------+
10 rows in set (0.01 sec)
Some of the products in the store are packages, which contain 3 other products. So I have a products_packages table to connect packages to their products:
Code:
mysql> describe products_packages;
+------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+-------+
| package_id | int(10) unsigned | NO | PRI | NULL | |
| product_id | int(10) unsigned | NO | PRI | NULL | |
+------------+------------------+------+-----+---------+-------+
2 rows in set (0.01 sec)
package_id and product_id are both ids from the products table.. ie, products.id.
I also have 2 different tables of related data for the different types of products: books and dvds:
Code:
mysql> describe books;
+-----------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+------------------+------+-----+---------+-------+
| product_id | int(10) unsigned | NO | PRI | NULL | |
| isbn10 | varchar(10) | YES | | NULL | |
| isbn13 | varchar(13) | YES | | NULL | |
| publisher_id | int(10) unsigned | YES | | NULL | |
| publishing_date | date | YES | | NULL | |
| pages | int(10) unsigned | YES | | NULL | |
| dimensions | varchar(255) | YES | | NULL | |
| format_id | int(10) unsigned | YES | | NULL | |
| preview_url | varchar(255) | YES | | NULL | |
+-----------------+------------------+------+-----+---------+-------+
9 rows in set (0.03 sec)
mysql> describe dvds;
+-------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+-------+
| product_id | int(10) unsigned | NO | PRI | NULL | |
| producer_id | int(10) unsigned | YES | MUL | NULL | |
+-------------+------------------+------+-----+---------+-------+
2 rows in set (0.03 sec)
books.product_id and dvds.product_id are foreign keys for products.id.
The main goal is to get all of the data for all products in one query. I can do this with the one to one relationships, but I'm having a hard time with the one to many, particularly the packages.
For example, for each package I'd like to have a row for each product in the package, each of which has the data for the package, and the data for the particular product.
ie, for each package, I'd like something like this:
Code:
package id, package name, package data (image, description, etc), product #1 id, product #1 name, product #1 data
package id, package name, package data (image, description, etc), product #2 id, product #2 name, product #2 data
package id, package name, package data (image, description, etc), product #3 id, product #3 name, product #3 data
Also note that the data for each individual product should include the data from the books and dvds tables.
How would I go about doing this? And if there's a better way to approach the problem, I'd like to know.
Thanks!