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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:56:07+00:00 2026-06-06T17:56:07+00:00

I have two MySQL tables: attributes (attributeid, name) productsattributes (productid, attributeid, displayvalue) The required

  • 0

I have two MySQL tables:

  • attributes (attributeid, name)
  • productsattributes (productid, attributeid, displayvalue)

The required is for each attribute name called “Product Type” get all other attributes associated with this “Product Type”. As an example — attributes table will look like:

    attributeid  name
    1            A
    2            B
    3            Product Type
    4            D

productsattributes table will look like:

productid attributeid displayvalue
        1         3           FAN
        1         1           Brown
        1         2           Stand
        2         3           FAN
        2         4           D
        3         3           CAR
        3         4           imported

So the final result should be:

FAN (A,B, Product Type,D) 
CAR (Product Type, imported)

Here is my try:

  • first I get all the “displayvalues” from productattributes:
SELECT DISTINCT displayvalue 
  FROM productsttributes 
 WHERE attributeid = 3;
  • then I loop through each “displayvalues” to find the other attributes:
SELECT a.name 
 FROM attributes a 
 INNER JOIN productsattributes pa 
    ON pa.attributeid = a.attributeid AND productid in (
        SELECT productid 
        FROM productsttributes 
        WHERE dispalyvale =  '$displayvalue') 
 ORDER BY a.name;

The problem is the productattributes table has about 7 million rows, so my script is taking forever .. of course I am not looking for 10 minutes solution but at least it will improve my queries a bit.

  • 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-06T17:56:10+00:00Added an answer on June 6, 2026 at 5:56 pm

    I would start with the following statements:

    ALTER TABLE attributes ADD CONSTRAINT p_attributes PRIMARY KEY (attributeid);
    ALTER TABLE productsattributes ADD CONSTRAINT p_productsattributes
        PRIMARY KEY(productid, attributeid);
    ANALYZE TABLE attributes, productsattributes;
    

    This will make sure all important fields are indexed.

    The query might look like this (also on SQL Fiddle):

    SELECT trg.displayvalue,
           group_concat(a.name ORDER BY trg.productid, a.attributeid)
      FROM (
            SELECT t.productid,t.displayvalue
              FROM attributes a
              JOIN productsattributes t USING (attributeid)
             WHERE a.name = 'Product Type') AS trg
      JOIN productsattributes p ON p.productid = trg.productid
      JOIN attributes a ON a.attributeid = p.attributeid
     GROUP BY trg.displayvalue
     ORDER BY 1;
    

    Please, kindly include the EXPLAIN output of your’s and this queries into your question.

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

Sidebar

Related Questions

I have two mysql tables called Product and Category Product id name category 1
I have two mySQL tables as follows: [product] table P_id | Name | Quantity
I have two tables in a MySql DB: items id (char) name (varchar) description
I have two MySQL tables (product and price history) that I would like to
I have two tables in MySQL that I'm comparing with the following attributes: tbl_fac
I have two MySQL tables: product has id , title , price and some
i have two mysql tables. the first is called chosen and consists of id,
I have two tables job which contains an attribute called employer_id_job that links it
I have two mysql tables that have the following structure: Table 1: ---ID---------NAME------- ---1-----
I have two MySQL tables, locations and items: locations `id` `name` `address` `latitude` `longitude`

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.