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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:19:51+00:00 2026-06-13T22:19:51+00:00

I’m trying to write a query that would extract products that have identical option

  • 0

I’m trying to write a query that would extract products that have identical option values assigned to them. Affected tables are:

products
| id |

rel_product_options
| product | option | value |
"option" is an id from "options" table
"value" is an option value id from "values" table (an option can have multiple values. eg. option "color" can have "red", "blue" etc.)

Every product has multiple option-value combinations ir “rel_product_options” table. Variations of the same product cannot have identical option-value pairs and that’s why prior to changing any value on any option i need to check if the next value would no conflict with some other product. Let’s assume

  • product A (color: red, size: 10)
  • product B (color: blue, size: 10)

Now if i try to change color of “product B” to “red” – i should be able to find in the database that options of “product A” would be identical and cancel the change. I hope the question is clear and honestly i don’t even know where to start: sql is something of a mystery to me no matter how many times i use it … I’ve started with something like the code below, but i think it is probably wrong on more levels than i can count to so any help is very appreciated.

# my thoughts
SELECT P1.*, PO1.*, PO2.* FROM products AS P1
# first select the product we want to extract all options from
# THIS ALREADY IS WRONG, because one of the options is ABOUT TO CHANGE, but this code assumes it has already done that
JOIN rel_product_options AS PO1 ON (PO1.product = P1.id AND PO1.product = 1)
# now join all other products that do not have identical option-value pairs
LEFT JOIN rel_product_options AS PO2 ON (PO1.product != PO2.product AND PO1.`option` = PO2.`option` AND PO1.value = PO2.value)
# and ... i'm lost ...
  • 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-13T22:19:52+00:00Added an answer on June 13, 2026 at 10:19 pm

    Untested, but try something like this to find duplicate products of THIS_PRODUCT_ID. Note I am assuming that each row in rel_product_options is unique.

    SELECT o2.product AS duplicateProduct 
    FROM rel_product_options o
    INNER JOIN rel_product_options o2 ON o.product <> o2.product 
        AND o.option = o2.option 
        AND o.value = o2.value
    WHERE o.product = THIS_PRODUCT_ID
    GROUP BY o2.product
    HAVING COUNT(*) = (SELECT COUNT(*) 
                       FROM rel_product_options o3 
                       WHERE o3.product = THIS_PRODUCT_ID)
    

    Edit: It looks like you are trying to put this in an ON UPDATE trigger for rel_product_options. This is a bit of a hack, but I suppose you could try something like the following. This is of course assuming that OLD.product = NEW.product otherwise all bets are off:

    SELECT o2.product AS duplicateProduct 
    FROM
    (
        SELECT product, 
            CASE WHEN option = OLD.option AND value = OLD.value THEN NEW.option ELSE option END AS option, 
            CASE WHEN option = OLD.option AND value = OLD.value THEN NEW.value ELSE value END AS value
        FROM rel_product_options
        WHERE product = NEW.product
    ) o
    INNER JOIN rel_product_options o2 ON o.product <> o2.product 
        AND o.option = o2.option 
        AND o.value = o2.value
    GROUP BY o2.product
    HAVING COUNT(*) = (SELECT COUNT(*) 
                       FROM rel_product_options o3 
                       WHERE o3.product = NEW.product)
    

    Edit 2: If you’re running this from PHP, changing a parameter’s value, you can try the following. This now assumes that (product, option) is unique, and the option already exists, with a different value. That is, you will be following up the query with an UPDATE to modify a row, not an INSERT.

    SELECT o2.product AS duplicateProduct 
    FROM
    (
        SELECT product, 
            option, 
            CASE WHEN option = @option THEN @newValue ELSE value END AS value
        FROM rel_product_options
        WHERE product = @product
    ) o
    INNER JOIN rel_product_options o2 ON o.product <> o2.product 
        AND o.option = o2.option 
        AND o.value = o2.value
    GROUP BY o2.product
    HAVING COUNT(*) = (SELECT COUNT(*) 
                       FROM rel_product_options o3 
                       WHERE o3.product = @product)
    

    There are three parameters in this query:

    • @product: The product ID in question
    • @option: The option you are changing
    • @newValue: The new value you will change the option to
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.