I have a table of 'product type attributes' which lists the names of all possible product attributes.
In another table I have the values for the particular attributes for a given product.
I need to query the database to get all attributes names for a specific product type and their values if they have any but also if they don't have a value assigned.
For example
Product type 1 : T-shirt
Product type 2 : Poster
ATTRIBUTES
ID | PRODUCT_TYPE_ID | ATTRIBUTE NAME
1 | 1 | Size
2 | 1 | Colour
3 | 1 | Style
4 | 2 | Print Type
5 | 2 | Paper Type
6 | 2 | Paper Size
In the table 'product_attributes' that holds the values for these attributes (that also links it to a particular product) I have :
Product 1 : Men's T-shirt
Product 2 : Large Poster
PRODUCT_ATTRIBUTES
ID | PRODUCT_ID | ATTRIBUTE_ID | VALUE
1 | 1 | 1 | Large
2 | 1 | 2 | Blue
3 | 1 | 3 | Mens
4 | 2 | 4 | Screen print
The result I want :
Return all the attributes for a product type, even though 'print type' is the only attribute with an assigned value for that particular product.
ID | PRODUCT_ID | ATTRIBUTE_ID | ATTRIBUTE_NAME | VALUE
1 | 2 | 4 | Print type | Screen print
2 | 2 | 5 | Paper type |
3 | 2 | 6 | Paper size |
I tried various JOINs but so far haven't got exactly what I want. The following returns values for other products not the specific one I need.
"SELECT
pa.value AS value,
pa.product_id AS product_id,
ptal.id AS attribute_id,
ptal.name AS attribute_name
FROM product_type_attribute_labels ptal
LEFT OUTER JOIN product_attributes pa ON ptal.id = pa.product_type_attribute_id
WHERE ptal.product_type_id = :ptid
GROUP BY ptal.id"
From what I have to this moment the following query can already provide some help
select a.id,pa.id,ATTRIBUTE_ID,`ATTRIBUTE NAME`,value from attributes a
join product_attributes pa on a.id=pa.ATTRIBUTE_ID
where product_id = 2
Change product_id accordingly
check this sqlfiddle
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.