[Solved] Aggregate data using two junction tables
Posted: Sat Aug 16, 2025 7:04 am
				
				This is going to take a bit of explaining, lol.  I'm working on my fanfic database, which has the main table Fanfics (containing primary key FanficID and a host of other specific fields), along with tables Fandoms, Authors, Chars, Ships, Themes, etc.  (The primary keys for those have similar logical labels like FandomID, AuthorID, etc.)  There are junction tables connecting most of these tables with Fanfics, and between the Authors and Ships table and Fandoms (the Chars table uses a foreign key to connect it to Fandoms).
Crucially, there are *two* junction tables connecting Fanfics and Chars, and Fanfics and Ships. For the ships, one is Fanfic_Ships, and the other is Fanfic_MShips (and the chars are named similarly). The data in these two junction tables is unique and never overlaps (a ship is never on both tables for the same fanfic), but I need to keep them separate for good reasons (they distinguish major vs. minor ships in a fic, a distinction that is very useful).
I've been using variations of the following query to pull data for a bunch of fanfics at once, aggregating all fandoms, authors, ships, and themes into a single field each.
It doesn't currently pull data for Chars but seeing how the ships are done, I could easily modify it to make it do that.  The problem is, I have absolutely no idea how to pull both the ships on Fanfic_Ships *and* Fanfic_MShips, and same for the Fanfic_Chars and Fanfic_MChars.  The way the joining is set up, it connects the Ships table to Fanfics by looking at the pairs on Fanfic_Ships, leaving no way to join and aggregate the pairs on Fanfic_MShips as well.  I'd love to be able to end up with separate columns for Ships, MShips, Chars, and MChars.  Does anyone have any ideas on how to do this, or know what I need to look into?
			Crucially, there are *two* junction tables connecting Fanfics and Chars, and Fanfics and Ships. For the ships, one is Fanfic_Ships, and the other is Fanfic_MShips (and the chars are named similarly). The data in these two junction tables is unique and never overlaps (a ship is never on both tables for the same fanfic), but I need to keep them separate for good reasons (they distinguish major vs. minor ships in a fic, a distinction that is very useful).
I've been using variations of the following query to pull data for a bunch of fanfics at once, aggregating all fandoms, authors, ships, and themes into a single field each.
Code: Select all
select
	group_concat(distinct fandom order by fandom separator ', ') as fandoms,
	group_concat(distinct author order by upper(author) separator ', ') as authors,
	title,
	wordcount,
	rating,
	link,
	group_concat(distinct ship separator ', ') as ships,
	group_concat(distinct theme separator ', ') as themes
from (
select
	"Fanfics"."FanficID" as id,
	"Fandoms"."Fandom" as fandom,
	"Authors"."Author" as author,
	"Fanfics"."Title" as title,
	"Fanfics"."Wordcount" as wordcount,
	"Fanfics"."Rating" as rating,
	"Fanfics"."WebLink" as link,
	ifnull("Ships"."Ship", 'no ships') as ship,
	ifnull("Themes"."Theme", 'no themes') as theme
from
	"Fanfics",
	"Fanfic_Author", "Authors",
	"Fanfic_Fandom", "Fandoms"
left join
	"Fanfic_Ships" on "Fanfic_Ships"."FanficID" = "Fanfics"."FanficID"
left join
	"Ships" on "Fanfic_Ships"."ShipID" = "Ships"."ShipID"
left join
	"Fanfic_Theme" on "Fanfic_Theme"."FanficID" = "Fanfics"."FanficID"
left join
	"Themes" on "Fanfic_Theme"."ThemeID" = "Themes"."ThemeID"
where
	    "Fanfic_Author"."FanficID" = "Fanfics"."FanficID"
	and "Fanfic_Author"."AuthorID" = "Authors"."AuthorID"
	and "Fanfic_Fandom"."FanficID" = "Fanfics"."FanficID"
	and "Fanfic_Fandom"."FandomID" = "Fandoms"."FandomID"
)
group by
	id, title, wordcount, rating, link
order by
	fandoms, upper(authors), upper(title)
;