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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:13:09+00:00 2026-06-11T05:13:09+00:00

I have a select query to build and I can’t get it work propery.

  • 0

I have a select query to build and I can’t get it work propery. I was hopeing for a few sudggestions from more advanced MySQL developers.
So my tables are:

 CREATE TABLE IF NOT EXISTS `gv` ( 
`id` int(11) NOT NULL AUTO_INCREMENT,
`option_id` int(11) NOT NULL,
`group_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

INSERT INTO `gv` (`id`, `option_id`, `group_id`) VALUES
(1, 1, 1),
(2, 2, 2),
(3, 3, 2),
(4, 4, 3),
(5, 5, 4),
(6, 6, 4); 

CREATE TABLE IF NOT EXISTS `igv` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `item_id` int(11) NOT NULL,
 `gv_id` int(11) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;

INSERT INTO `igv` (`id`, `item_id`, `gv_id`) VALUES
(1, 1, 1),
(2, 1, 3),
(3, 2, 1),
(4, 2, 2),
(6, 3, 5),
(7, 4, 2),
(8, 2, 6); 

CREATE TABLE IF NOT EXISTS `items` (
  `item_id` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`item_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

INSERT INTO `items` (`item_id`) VALUES
(1),
(2),
(3),
(4),
(5); 

Now I will explain what these tables are doing:

  1. gv comes from group_values. A group is like a property for an item. Ex: Color.

    • each row holds values for a group. Ex: Red ( option_id ) for group Color ( group_id ).
  2. igv comes from item group values. An item is the main entity in my project, like a product.

    • each row holds relations between items and group_values. Ex: An item with Red Color.
    • it’s linked to the gv table by gv_id ( igv.gv_id = gv.id ).
  3. items comes from items 🙂 I have simplified for this example to only one column, item_id.

What I need to query:

Well, in my app I have a selected item ( my_item ) ( with 0 or more group values attached ).
I need all the items that have exactly the same group_values defined, or no values specified for my_item groups. In php I can extract all the attached groups as an array with ids, also all the group_values. But I found no way to select what I need to from this database.
I will appreciate very much any input provided into this massive time-consumming select.

Thanks!

A short sketched-up example. On the left we have the item that is matching all the right items ( separated with comma ).
enter image description here

  • 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-11T05:13:11+00:00Added an answer on June 11, 2026 at 5:13 am

    OK, so to start with we need to find all items with a group matching another item:

    SELECT igv.item_id, sigv.item_id FROM igv
      INNER JOIN gv ON igv.gv_id = gv.id
      INNER JOIN gv sgv ON gv.group_id = sgv.group_id
      INNER JOIN igv sigv ON sigv.gv_id = sgv.id
      WHERE igv.item_id = items.item_id
        AND sigv.item_id = similar.item_id
    

    We then are only interested when the option does not match:

    SELECT igv.item_id, sigv.item_id dissimilar FROM igv
      INNER JOIN gv ON igv.gv_id = gv.id
      INNER JOIN gv sgv ON gv.group_id = sgv.group_id
      INNER JOIN igv sigv ON sigv.gv_id = sgv.id
      WHERE igv.item_id = items.item_id
        AND sigv.item_id = similar.item_id
        AND gv.option_id != sgv.option_id
    

    Then we are only interested in the items that are not selected above. This can be done using either NOT EXISTS or a LEFT JOIN. Here’s how to do it with NOT EXISTS:

    SELECT items.item_id, similar.item_id similar_id
      FROM items similar
      INNER JOIN items
      WHERE items.item_id != similar.item_id
        AND NOT EXISTS (
        SELECT igv.item_id, sigv.item_id dissimilar FROM igv
          INNER JOIN gv ON igv.gv_id = gv.id
          INNER JOIN gv sgv ON gv.group_id = sgv.group_id
          INNER JOIN igv sigv ON sigv.gv_id = sgv.id
          WHERE sigv.item_id = similar.item_id
            AND igv.item_id = items.item_id
            AND gv.option_id != sgv.option_id
        )
    

    Result:

    ITEM_ID SIMILAR_ID
    1       3
    1       5
    2       4
    2       5
    3       1
    3       4
    3       5
    4       2
    4       3
    4       5
    5       1
    5       2
    5       3
    5       4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SELECT query that I am expecting millions of results from. I
I have an application where users can select a variety of interests from around
if i have a query like : SELECT * FROM table WHERE id IN
I want to build a query like SELECT username from users WHERE login_attempts >
i'm trying to build a join, but I can't get it to work in
Imagine I have the following SQL query: SELECT id,name FROM user WHERE id IN
I have a select query in mysql . On each execution of this query
I have a select query which returns a list of data and I need
I have a select query. I want to replace that select query with another
Dear all, I have a select query that currently produces the following results: DoctorName

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.