Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3636726
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:04:10+00:00 2026-05-19T01:04:10+00:00

So I have written a query that will grab an order (this is for

  • 0

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(8)       | 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(8)      | NO   | PRI | NULL    | auto_increment |
| eoi_parentid | int(8)      | NO   |     | NULL    |                |
| eoi_type     | varchar(32) | NO   |     | NULL    |                |
| eoi_p_id     | int(8)      | NO   |     | NULL    |                |
| eoi_po_id    | int(8)      | NO   |     | NULL    |                |
| eoi_quantity | int(4)      | NO   |     | NULL    |                |
+--------------+-------------+------+-----+---------+----------------+

c_print_options

+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| po_id      | int(8)       | 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(8)       | NO   | PRI | NULL    | auto_increment |
| i_filename   | varchar(255) | NO   |     | NULL    |                |
| i_data       | longtext     | NO   |     | NULL    |                |
| i_parentid   | int(8)       | NO   |     | NULL    |                |
+--------------+--------------+------+-----+---------+----------------+
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T01:04:11+00:00Added an answer on May 19, 2026 at 1:04 am

    This is how I’d write the first query. I prefer to use joins.

    SELECT eoi_parentid, eoi_p_id, eoi_po_id, eoi_quantity, i_id, i_parentid, po_name, po_price
    FROM ecom_order_items
    INNER JOIN images
        ON i_id = eoi_p_id
    INNER JOIN c_print_options 
        ON po_id = eoi_po_id
    WHERE eoi_parentid = '1'
    

    For your second query I would use a UNION on two queries, one for images and one for products.

    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
    INNER JOIN images
        ON i_id = eoi_p_id
    INNER JOIN c_print_options
        ON po_id = eoi_po_id
    WHERE eoi_type = 'image' AND i_id = eoi_p_id --Image conditions
      AND eoi_parentid = '9'   
      AND po_id = eoi_po_id
    
    UNION
    
    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
    INNER JOIN images
        ON i_id = eoi_p_id
    INNER JOIN c_print_options
        ON po_id = eoi_po_id
    WHERE eoi_type = 'product' AND ep_id = eoi_p_id -- Product conditions
      AND eoi_parentid = '9' 
      AND po_id = eoi_po_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a complex query that will return me a list of IDs.
I have written a LINQ to XML query that does what I want, but
I have written an AIR Application that downloads videos and documents from a server.
I have defined a web service that will return the data from my mysql
I have recently written a socket server in PHP that will be handling communication
I have written some PHP code that will return a value if there are
I have a query filter written in human readable language. I need to parse
I have a large query (not written by me, but I'm making some modifications).
I have written a site in Prototype but want to switch to jQuery. Any
I have written an AppleScript which when supplied with a Windows network link, will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.