Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL, three tables: Select all rows in right table including rows that are not mapped in middle table

My schema are as follows:

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          |

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".

So the results will look like this:

| 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       |

-- EDIT -- Additional Information.

I originally created a pivot table using this article:

http://dev.mysql.com/tech-resources/articles/wizard/print_version.html

Based on that article, I wrote a SQL statement to dynamically generate a SQL query that generates the pivot table.

That statement looked like this:

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

The second SQL statement was as follows:

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

The results looked like this:

| 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    |

I tried to repurpose the SQL and the concepts in these two statements, but I am at a loss.

like image 385
JDS Avatar asked Dec 30 '25 18:12

JDS


1 Answers

CROSS JOIN can be used here. (The first query was posted first by @nick rulez):

SELECT s.Id   AS SiteId 
     , s.Url  AS SiteURL 
     , f.FeatureName 
     , CASE WHEN sf.SiteID = s.Id 
            THEN 1
            ELSE 0
       END AS Enabled
FROM
    Sites AS s
  CROSS JOIN
    Features AS f
  LEFT JOIN
    SiteFeatures AS sf
      ON  sf.SiteID = s.Id
      AND sf.FeatureID = f.Id

SELECT s.Id   AS SiteId 
     , s.Url  AS SiteURL 
     , f.FeatureName 
     , CASE WHEN EXISTS
                 ( SELECT *
                   FROM SiteFeatures AS sf
                   WHERE sf.SiteID = s.Id
                     AND sf.FeatureID = f.Id
                 ) 
            THEN 1
            ELSE 0
       END AS Enabled
FROM
    Sites AS s
  CROSS JOIN
    Features AS f

SELECT s.Id   AS SiteId 
     , s.Url  AS SiteURL 
     , f.FeatureName 
     , ( SELECT COUNT(*)
         FROM SiteFeatures AS sf
         WHERE sf.SiteID = s.Id
           AND sf.FeatureID = f.Id
       ) AS Enabled
FROM
    Sites AS s
  CROSS JOIN
    Features AS f
like image 111
ypercubeᵀᴹ Avatar answered Jan 02 '26 11:01

ypercubeᵀᴹ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!