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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:47:49+00:00 2026-05-13T23:47:49+00:00

Update: After playing around with this for a few hours, went with a multi-query

  • 0

Update: After playing around with this for a few hours, went with a multi-query solution and used a table that only contained parent attributes to determine which items needed updating.


Sorry for the poor title, I couldn’t think how to concisely describe this problem.

I have a set of items that should have a 1-to-1 relationship with an attribute.

I have a query to return those rows where the data is wrong and this relationship has been broken (1-to-many). I’m gathering these rows to fix them and restore this 1-to-1 relationship.

This is a theoretical simplification of my actual problem but I’ll post example table schema here as it was requested.

item table:

+------------+------------+-----------+
| item_id    | name       | attr_id   |
+------------+------------+-----------+
| 1          | BMW 320d   | 20        |
| 1          | BMW 320d   | 21        |
| 2          | BMW 335i   | 23        |
| 2          | BMW 335i   | 34        |
+------------+------------+-----------+

attribute table:

+---------+-----------------+------------+
| attr_id | value           |  parent_id |
+---------+-----------------+------------+
|   20    | SE              |         21 | 
|   21    | M Sport         |          0 |
|   23    | AC              |         24 |
|   24    | Climate control |          0 |
              ....
|   34    | Leather seats   |          0 |
+---------+-----------------+------------+

A simple query to return items with more than one attribute.

SELECT item_id, COUNT(DISTINCT(attr_id)) AS attributes 
FROM item GROUP BY item_id HAVING attributes > 1

This gets me a result set like so:

+-----------+------------+
|   item_id | attributes |
+-----------+------------+
|    1      |          2 |
|    2      |          2 |
|    3      |          2 |
        -- etc. --

However, there’s an exception. The attribute table can hold a tree structure, via parent links in the table. For certain rows, parent_id can hold the ID of another attribute. There’s only one level to this tree. Example:

+---------+-----------------+------------+
| attr_id | value           |  parent_id |
+---------+-----------------+------------+
|   20    | SE              |         21 |
|   21    | M Sport         |          0 |
              ....

I do not want to retrieve items in my original query where, for a pair of associated attributes, they related like attributes 20 & 21.

I do want to retrieve items where:

  • the attributes have no parent
  • for two or more attributes they are not related (e.g. attributes 23 & 34)

Example result desired, just the item ID:

+------------+
| item_id    |
+------------+
| 2          |
+------------+

How can I join against attributes from items and exclude these rows?

Do I use a temporary table or can I achieve this from a single query?

Thanks.

  • 1 1 Answer
  • 1 View
  • 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-13T23:47:50+00:00Added an answer on May 13, 2026 at 11:47 pm

    The following query will extract only unique pairs of item and attribute (or its parent if any), thus eliminating the duplicates (this is per your request that an attribute can have only one parent, and the parent has no parents).

    SELECT DISTINCT I.item_id AS iid, A.par_id AS aid
    FROM 
        items AS I, 
        (SELECT AA.attr_id, IF(AA.parent_id = 0, AA.attr_id, AA.parent_id) AS par_id 
         FROM attribute AS AA) AS A
    WHERE I.attr_id = A.attr_id
    ORDER BY I.item_id
    

    So, using the above query as a subtable for your counting query will work (same approach I used with the A subtable above):

    SELECT SUB.iid, COUNT(DISTINCT(SUB.aid)) AS attributes
    FROM
        (SELECT DISTINCT I.item_id AS iid, A.par_id AS aid
         FROM 
            items AS I, 
            (SELECT AA.attr_id, IF(AA.parent_id = 0, AA.attr_id, AA.parent_id) AS par_id 
             FROM attribute AS AA) AS A
         WHERE I.attr_id = A.attr_id
         ORDER BY I.item_id) AS SUB
    GROUP BY SUB.iid
    HAVING attributes > 1
    

    I have added 3 more rows to your example items table, to accommodate the case, where an item can be linked only to an attribute with parent, but not the parent itself (i.e. item 3 -> 23 and 3 -> 20), and 4 -> 23.

    Running the above query lists only items 2 and 3 with 2 attributes each.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Update: After some more reading I see that this problem is totally general, you
I'm playing around with some image filters using Canvas after reading this post this
UPDATE: After searching a bit I don't seem to be alone about this problem:
I'm trying to write an after update trigger that does a batch update on
I have a table which is updated with ajax and after update it if
EDIT - After playing around with a bunch of potential solutions (using backgroundworker and
I'm playing around with Ruby's .object_id and noticed that, in several sequential sessions of
I've been playing around with creating a new user and I keep getting this
Another day playing around with joomla, and another shortcoming to fix :) This time
I've been playing around a bit with Minitest, and have found behavior that 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.