Sql - How to load the "Attribute Value"

Ihandler 21 Reputation points
2020-08-29T06:17:05.457+00:00

attribute [table]

  • attribute_id
  • attribute_name
  • category_id

product_attribute [table]

  • group_id
  • attribute_id
  • attribute_value

What's the sql statment should be if I want to have a table as like below

Attribute Name Attribute Value

CPU Model
CPU Speed
Memory Size
HDD Size

Developer technologies | Transact-SQL
{count} votes

3 answers

Sort by: Most helpful
  1. Guoxiong 8,206 Reputation points
    2020-08-29T20:46:26.407+00:00

    You can use INNER JOIN to list both columns:

    SELECT a.attribute_name AS [Attribute Name], p.attribute_value AS [Attribute Name]
    FROM attribute AS a
    INNER JOIN product_attribute AS p ON a.attribute_id = p.attribute_id;
    
    0 comments No comments

  2. Ihandler 21 Reputation points
    2020-08-30T06:38:47.443+00:00

    Sorry but no, your query does not include "category_id" and "group_id". And this does show the correct result.

    What I am looking for is something like this but the following query does not show any data once the "product_attribute" table has no data included.

    SELECT a.attribute_name, p.attribute_value
    FROM attribute AS a
    INNER JOIN item_attribute AS p ON a.attribute_id = p.attribute_id
    WHERE a.category_id = 123 AND p.group_id = 10

    0 comments No comments

  3. EchoLiu-MSFT 14,621 Reputation points
    2020-08-31T07:36:59.37+00:00

    Hi @Ihandler ,

    Left join can return all the values of the left table (even if there is no matching value in the right table),please refer to below sql:

    SELECT  a.attribute_name [Attribute Name],a. category_id,b.group_id,b.attribute_value [Attribute Value]  
    FROM attribute  a  
    left JOIN product_attribute p   
    ON a.attribute_id = p.attribute_id  
    

    If this doesn't solve your problem,please share us your table structure (CREATE TABLE …) and some sample data(INSERT INTO …)along with your expected result? So that we’ll get a right direction and make some test.

    If you have any question, please feel free to let me know.
    If the response is helpful, please click "Accept Answer" and upvote it.

    Best Regards
    Echo

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.