Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL, three tables: Select all rows in right table including rows that are not mapped in middle table
    primarykey
    data
    text
    <p>My schema are as follows:</p> <pre><code>Sites S | S.Id | S.Url | | 1 | a.com | | 2 | b.edu | | 3 | c.org | SiteFeatures SF | SF.SiteId | SF.FeatureID | | 1 | 1 | | 1 | 2 | | 1 | 3 | | 2 | 1 | | 2 | 2 | | 2 | 3 | | 2 | 4 | | 3 | 2 | | 3 | 3 | Features F | F.Id | F.FeatureName | | 1 | apple | | 2 | banana | | 3 | cherry | | 4 | diaper | | 5 | egg | | 6 | fish | </code></pre> <p>I want to select all sites, mapped to all features, including features missing from the middle join table. For features that have no entry in the join table, I want to display a "0". For features that exist in the join table, I want a "1".</p> <p>So the results will look like this:</p> <pre><code>| SiteId | SiteURL | FeatureName | Enabled | | 1 | a.com | apple | 1 | | 1 | a.com | banana | 1 | | 1 | a.com | cherry | 1 | | 1 | a.com | diaper | 0 | | 1 | a.com | egg | 0 | | 1 | a.com | fish | 0 | | 2 | b.edu | apple | 1 | | 2 | b.edu | banana | 1 | | 2 | b.edu | cherry | 1 | | 2 | b.edu | diaper | 1 | | 2 | b.edu | egg | 0 | | 2 | b.edu | fish | 0 | | 3 | c.org | apple | 0 | | 3 | c.org | banana | 1 | | 3 | c.org | cherry | 0 | | 3 | c.org | diaper | 1 | | 3 | c.org | egg | 0 | | 3 | c.org | fish | 0 | </code></pre> <p>-- EDIT -- Additional Information.</p> <p>I originally created a pivot table using this article:</p> <p><a href="http://dev.mysql.com/tech-resources/articles/wizard/print_version.html" rel="nofollow">http://dev.mysql.com/tech-resources/articles/wizard/print_version.html</a></p> <p>Based on that article, I wrote a SQL statement to dynamically generate a SQL query that generates the pivot table.</p> <p>That statement looked like this:</p> <pre><code>SELECT CONCAT( ", SUM(IF( F.FeatureName = '" , F.FeatureName , "', 1,0 ))" , " AS `" , F.FeatureName , "` ") AS CutNPaste FROM Features F WHERE 1 GROUP BY F.FeatureName ORDER BY F.FeatureName -- END </code></pre> <p>The second SQL statement was as follows:</p> <pre><code>SELECT , S.Url /* This section was dynamically generated, and copied into this SELECT statement */ , SUM(IF( F.FeatureName = 'apple', 1,0 )) AS `apple` , SUM(IF( F.FeatureName = 'banana', 1,0 )) AS `banana` , SUM(IF( F.FeatureName = 'cherry', 1,0 )) AS `cherry` , SUM(IF( F.FeatureName = 'diaper', 1,0 )) AS `diaper` , SUM(IF( F.FeatureName = 'egg', 1,0 )) AS `egg` , SUM(IF( F.FeatureName = 'fish', 1,0 )) AS `fish` /* END of dynamic part */ FROM Sites S LEFT OUTER JOIN SiteFeatures SF ON S.Id = SF.SiteId LEFT OUTER JOIN Features F ON SF.FeatureId = F.Id WHERE 1 AND SF.FeatureId = F.Id AND S.Enabled = 1 GROUP BY S.Url -- END </code></pre> <p>The results looked like this:</p> <pre><code>| Url | apple | banana | cherry | diaper | egg | fish | | a.com | 1 | 1 | 1 | 0 | 0 | 0 | | b.edu | 1 | 1 | 1 | 1 | 0 | 0 | | c.org | 0 | 1 | 0 | 1 | 0 | 0 | </code></pre> <p>I tried to repurpose the SQL and the concepts in these two statements, but I am at a loss.</p>
    singulars
    1. This table or related slice is empty.
    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