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