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 7959945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:39:06+00:00 2026-06-04T04:39:06+00:00

To make a long story short, I have some mildly complex queries (see example

  • 0

To make a long story short, I have some mildly complex queries (see example below) and need to save their results into a table rather than running them every time. I have about 6 months of experience with MYSQL, so I know just enough to get myself into trouble.

Background: Each product has multiple rules that determine pricing, shipping, etc. The kitruleoptions table connects each rule to the dropdown options it will apply for — so if a customer selects “Add optional X,” the rules pertaining to Xs get applied. The optionsets table connects each ProductItemID to the dropdown options it is associated with. So, a rule should apply for a particular product item only if all of the product item’s dropdown options are found in that rule’s set of dropdown options. In other words, it’s not that the rule necessarily applies every time X is selected, but rather, it may apply every time X is selected in dropdown A and either Y or Z are selected in dropdown B, but not if W is selected in dropdown B.

Here is an example query that gets me the total price for an individual ProductItemID.

SELECT ProductItemID, SUM(RulePrice * ItemQuantity) AS Price FROM 
(SELECT RuleGroups, ProductItemGroups, RulePrice, ItemQuantity, productitems.ProductItemID
FROM kitrules
LEFT JOIN productitems ON productitems.ProductID = kitrules.ProductID
LEFT JOIN (SELECT RuleID, GROUP_CONCAT(OptionGroupID ORDER BY OptionGroupID) AS RuleGroups 
    FROM kitruleoptions
    WHERE OptionGroupID IN (SELECT OptionGroupID FROM optionsets WHERE ProductItemID = %s)
    GROUP BY RuleID) AS k ON k.RuleID = kitrules.RuleID
LEFT JOIN (SELECT ProductItemID, GROUP_CONCAT(OptionGroupID ORDER BY OptionGroupID) AS ProductItemGroups
    FROM optionsets
    GROUP BY ProductItemID) AS o ON o.ProductItemID = productitems.ProductItemID
WHERE productitems.ProductItemID = %s AND RuleGroups = ProductItemGroups) AS x
GROUP BY x.ProductItemID

Currently, this select (or rather, a more complex one that selects more than just the price) goes inside an INSERT INTO statement somewhat like this:

INSERT INTO pricelists (ProductItemID, Price) 
SELECT [the query above] 
WHERE ProductItemID = %s

I am trying to formulate it instead something like this:

INSERT INTO pricelists (ProductItemID, Price)
SELECT [a new query that only requires ProductID]
WHERE ProductItemID IN (SELECT ProductItemID FROM productitems WHERE ProductID = %s)

… and only need to input ProductID in order to update the entire product at once. That way, I would no longer have to loop through a PHP array to do it. ProductID is found in both the kitrules table and in the productitems table, so I feel like there should be a way, but I can’t seem to find it. So my questions are, how do I reformulate the query to only need ProductID? Is it even possible? Is there a way to directly compare the set of optionsets and the set of kitruleoptions without the GROUP_CONCAT thing? Also, how would I set this up with a trigger, and what should it trigger on? I’ve never done triggers before.

EDIT: Here is my SQL Fiddle which has a simple example of how the tables are supposed to end up. The idea is that I started out with a setup where each product simply had one or more items, but then due to a late change in requirements (yay), I had to add in the ability to generate items that are kits, or combinations of multiple different items, and yet the kits are items themselves. The requirement is that a kit’s ItemCost is the sum of its contained items’ ItemCosts, and a kit’s Price is the sum of its contained rules’ RulePrices times ItemQuantity, and its shipping is determined by the combined shipping of its contained items’ shipping information, but otherwise, a kit is just another item. Cue three weeks of headache and counting.

  • 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-06-04T04:39:07+00:00Added an answer on June 4, 2026 at 4:39 am

    I figured it out eventually:

    SELECT ProductItemID, SUM(RulePrice * ItemQuantity) AS TotalPrice FROM
    (SELECT p.ProductItemID, k.RuleID, p_count, COUNT(k.RuleID) AS k_count,
    RulePrice, ItemQuantity
    FROM productitems
    INNER JOIN optionsets o ON productitems.ProductItemID = o.ProductItemID
    INNER JOIN (SELECT ProductItemID, COUNT(OptionGroupID) AS p_count 
            FROM optionsets
            GROUP BY ProductItemID) AS p ON p.ProductItemID = o.ProductItemID
    LEFT JOIN kitruleoptions k ON k.OptionGroupID = o.OptionGroupID
    LEFT JOIN kitrules ON k.RuleID = kitrules.RuleID
    WHERE productitems.ProductID = %s
    GROUP BY o.ProductItemID, k.RuleID
    HAVING k_count = p_count) AS x
    GROUP BY x.ProductItemID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking for some assistance. Long story short, I have most of my
To make a long story short I have to use processing in a class
It's a rather long story but to make it short I need to store
To make a long story short, I have dynamic pages on a website that
So to make a long story short. A professor asked this question in class
Short story: I can't make precompiled headers work properly with gcc -c option. Long
I want to make a long list short by hiding some elements in long
Long story short, I'm trying to write some toast-style popup notifications (similar to Growl)
I'll try to make a long story short: I want to call a method
Long story short, I have a substantial Python application that, among other things, does

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.