Reputation: 1646
I am trying to write a SQL query that pulls from 3 tables and haven't been able to achieve the results I need. I want to return all the property names (labels) that are paired with a particular category and pair those properties with the values from a particular asset from the said category, which may not have a value to pair with each property.
I want all the properties for category 4, paired with the values for asset 135. Asset 135 doesn't have values set for property 3 or 4, so I want:
| property_name | property_value |
|:--------------|:-------------------|
| Fixture ID | A5 |
| Manufacturer | Black & Decker |
| Model # | |
| Type | |
Here is my query:
SELECT property.property_name, asset_property.property_value
FROM property
LEFT OUTER JOIN category_property
ON property.property_id = category_property.property_id
INNER JOIN asset_property
ON asset_property.property_id = property.property_id
WHERE category_property.category_id = 4
AND asset_property.asset_id = 135
resulting data:
| property_name | property_value |
|:--------------|:-------------------|
| Fixture ID | A5 |
| Manufacturer | Black & Decker |
It's missing 2 of the properties because of my AND asset_property.asset_id = 135
Here are the tables: * asterisk just indicates rows from test case.
table: property
| property_id | property_name |
|:-----------:|:--------------|
| 1 | Fixture ID |*
| 2 | Manufacturer |*
| 3 | Model # |*
| 4 | Type |*
| 5 | Lamp Type |
table: asset_property
pairs assets with property values.
| asset_id | property_id | property_value |
|:--------:|:-----------:|:---------------|
| 129 | 1 | A5 |
| 129 | 2 | Black & Decker |
| 129 | 3 | 1230-02 |
| 129 | 4 | Incandescent |
| 135 | 1 | E6 |*
| 135 | 2 | Linden |*
| 147 | 1 | G1 |
table: category_property
pairs categories with its properties.
| category_id | property_id |
|:-----------:|:-----------:|
| 4 | 1 |*
| 4 | 2 |*
| 4 | 3 |*
| 4 | 4 |*
| 7 | 2 |
| 7 | 5 |
I've tried all the different JOIN types, HAVING, GROUP BY....can't it figure out. If anyone understand what I need, I'd really appreciate the help! Thanks!
To clarify, I need to return these sets of results:
| property_name |
|:--------------|
| Fixture ID |
| Manufacturer |
| Model # |
| Type |
AND
| property_value |
|:---------------|
| A5 |
| Black & Decker |
And join them on property_id
:
| property_name | property_value |
|:--------------|:---------------|
| Fixture ID | A5 |
| Manufacturer | Black & Decker |
| Model # |
| Type |
But since there is no record for property_id
4 and 5 in the asset_property table
, the property_name
for those properties aren't shown.
| property_name | property_value |
|:--------------|:---------------|
| Fixture ID | A5 |
| Manufacturer | Black & Decker |
Model #
Type
I want them to e shown. I want ALL the property names for Category 4 to be returned and be paired with any matching property_values for Asset 135.
So for the Fruits Category I want all the properties names returned (Color, Season, Taste). The color property for Apple is not set, but I still want all the property types to be returned, so they can be changed or filled out:
| property_name | property_value |
|:--------------|:---------------|
| Color | |
| Season | Fall |
| Taste | Tart |
Upvotes: 3
Views: 227
Reputation: 183311
Just change the INNER JOIN
to a LEFT OUTER JOIN
, and move AND asset_property.asset_id = 135
from the WHERE
clause to the ON
clause:
SELECT property.property_name,
asset_property.property_value
FROM property
INNER
JOIN category_property
ON property.property_id = category_property.property_id
LEFT
OUTER
JOIN asset_property
ON asset_property.property_id = property.property_id
AND asset_property.asset_id = 135
WHERE category_property.category_id = 4
;
(I've also changed the first LEFT OUTER JOIN
to an INNER JOIN
, since the WHERE category_property.category_id = 4
will (correctly) filter out records where the join fails, anyway.)
Upvotes: 5