Jump to content

4 table query, getting dupe rows and confused!


Recommended Posts

So I have written a query that will grab an order (this is for an ecommerce type site), and from that order id it will get all order items (ecom_order_items), print options (c_print_options) and images (images). The eoi_p_id is currently a foreign key from the images table.

 

This works fine and the query is:

 

SELECT
eoi_parentid, eoi_p_id, eoi_po_id, eoi_quantity,
i_id, i_parentid,
po_name, po_price
FROM ecom_order_items, images, c_print_options WHERE eoi_parentid = '1' AND i_id = eoi_p_id AND po_id = eoi_po_id;

 

The above would grab all the stuff I need for order #1

 

Now to complicate things I added an extra table (ecom_products), which needs to act in a similar way to the images table. The eoi_p_id can also point at a foreign key in this table too. I have added an extra field 'eoi_type' which will either have the value 'image', or 'product'.

 

Now items in the order could be made up of a mix of items from images or ecom_products. Whatever I try it either ends up with too many records, wont actually output any with eoi_type = 'product', and just generally wont work. Any ideas on how to achieve what I am after? Can provide SQL samples if needed?

 

SELECT
eoi_id, eoi_parentid, eoi_p_id, eoi_po_id, eoi_po_id_2, eoi_quantity, eoi_type,
i_id, i_parentid,
po_name, po_price, po_id,
ep_id
FROM ecom_order_items, images, c_print_options, ecom_products WHERE eoi_parentid = '9' AND i_id = eoi_p_id AND po_id = eoi_po_id

 

The above outputs duplicate rows and doesnt work as expected. Am I going about this the wrong way? Should I have seperate foreign key fields for the eoi_p_id depending it its an image or a product?

 

Should I be using JOINs?

 

Here is a mysql explain of the tables in question

 

ecom_products

+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| ep_id       | int(       | NO   | PRI | NULL    | auto_increment |
| ep_title    | varchar(255) | NO   |     | NULL    |                |
| ep_link     | text         | NO   |     | NULL    |                |
| ep_desc     | text         | NO   |     | NULL    |                |
| ep_imgdrop  | text         | NO   |     | NULL    |                |
| ep_price    | decimal(6,2) | NO   |     | NULL    |                |
| ep_category | varchar(255) | NO   |     | NULL    |                |
| ep_hide     | tinyint(1)   | NO   |     | 0       |                |
| ep_featured | tinyint(1)   | NO   |     | 0       |                |
+-------------+--------------+------+-----+---------+----------------+

ecom_order_items

+--------------+-------------+------+-----+---------+----------------+
| Field        | Type        | Null | Key | Default | Extra          |
+--------------+-------------+------+-----+---------+----------------+
| eoi_id       | int(      | NO   | PRI | NULL    | auto_increment |
| eoi_parentid | int(      | NO   |     | NULL    |                |
| eoi_type     | varchar(32) | NO   |     | NULL    |                |
| eoi_p_id     | int(      | NO   |     | NULL    |                |
| eoi_po_id    | int(      | NO   |     | NULL    |                |
| eoi_quantity | int(4)      | NO   |     | NULL    |                |
+--------------+-------------+------+-----+---------+----------------+

c_print_options

+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| po_id      | int(       | NO   | PRI | NULL    | auto_increment |
| po_name    | varchar(255) | NO   |     | NULL    |                |
| po_price   | decimal(6,2) | NO   |     | NULL    |                |
+------------+--------------+------+-----+---------+----------------+

images

+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| i_id         | int(       | NO   | PRI | NULL    | auto_increment |
| i_filename   | varchar(255) | NO   |     | NULL    |                |
| i_data       | longtext     | NO   |     | NULL    |                |
| i_parentid   | int(       | NO   |     | NULL    |                |
+--------------+--------------+------+-----+---------+----------------+

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.