Note that there are some explanatory texts on larger screens.

plurals
  1. PO3 Table, SQL Query
    primarykey
    data
    text
    <p>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.</p> <p>I want <em>all</em> the properties for <strong>category 4</strong>, paired with the values for <strong>asset 135</strong>. Asset 135 doesn't have values set for property 3 or 4, so I want:</p> <pre><code>| property_name | property_value | |:--------------|:-------------------| | Fixture ID | A5 | | Manufacturer | Black &amp; Decker | | Model # | | | Type | | </code></pre> <p>Here is my query: </p> <pre><code>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 </code></pre> <p><strong>resulting data</strong>: </p> <pre><code>| property_name | property_value | |:--------------|:-------------------| | Fixture ID | A5 | | Manufacturer | Black &amp; Decker | </code></pre> <p>It's missing 2 of the properties because of my <code>AND asset_property.asset_id = 135</code></p> <p>Here are the <strong>tables</strong>: * asterisk just indicates rows from test case. </p> <p>table: <strong>property</strong> </p> <pre><code>| property_id | property_name | |:-----------:|:--------------| | 1 | Fixture ID |* | 2 | Manufacturer |* | 3 | Model # |* | 4 | Type |* | 5 | Lamp Type | </code></pre> <p>table: <strong>asset_property</strong><br> pairs assets with property values. </p> <pre><code>| asset_id | property_id | property_value | |:--------:|:-----------:|:---------------| | 129 | 1 | A5 | | 129 | 2 | Black &amp; Decker | | 129 | 3 | 1230-02 | | 129 | 4 | Incandescent | | 135 | 1 | E6 |* | 135 | 2 | Linden |* | 147 | 1 | G1 | </code></pre> <p>table: <strong>category_property</strong><br> pairs categories with its properties. </p> <pre><code>| category_id | property_id | |:-----------:|:-----------:| | 4 | 1 |* | 4 | 2 |* | 4 | 3 |* | 4 | 4 |* | 7 | 2 | | 7 | 5 | </code></pre> <p>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!</p> <hr> <p>To clarify, I need to return these sets of results:</p> <pre><code>| property_name | |:--------------| | Fixture ID | | Manufacturer | | Model # | | Type | </code></pre> <p>AND </p> <pre><code>| property_value | |:---------------| | A5 | | Black &amp; Decker | </code></pre> <p>And join them on <code>property_id</code>:</p> <pre><code>| property_name | property_value | |:--------------|:---------------| | Fixture ID | A5 | | Manufacturer | Black &amp; Decker | | Model # | | Type | </code></pre> <p>But since there is no record for <code>property_id</code> 4 and 5 in the <code>asset_property table</code>, the <code>property_name</code> for those properties aren't shown.</p> <pre><code>| property_name | property_value | |:--------------|:---------------| | Fixture ID | A5 | | Manufacturer | Black &amp; Decker | </code></pre> <p><del>Model #<br> Type</del></p> <p>I want them to e shown. I want <em>ALL</em> the property names for <strong>Category 4</strong> to be returned and be paired with any matching property_values for <strong>Asset 135</strong>.</p> <hr> <p>So for the <strong>Fruits Category</strong> 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:</p> <pre><code>| property_name | property_value | |:--------------|:---------------| | Color | | | Season | Fall | | Taste | Tart | </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload