In case you have no time or interest in reading the whole post with the evolution of the database, below is a summary of my issue, starting with a brief outline of the tables involved:
I created a database to store information about fanfics (stories, often called fics) I had saved, and the main table is Fanfics, which stores the title and some other info about them. The relationship between fanfics and authors is many-to-many, so there is a junction table joining the Fanfics table with the Authors table called Fanfic_Author. (Currently this hasn't contributed to this question only because I only have a single fanfic entered that has more than one author, and only a couple test queries that would pull up that fanfic, and I haven't run those since the very beginnings of testing queries. Now I realize this is also a major issue, but I'll get to that later.)
The relationship between Fanfics and Fandoms is similar, with a Fandoms table and a Fanfic_Fandom junction table. Most fanfics only have one fandom (though those with more than one may prove to be an issue the same way the authors do), and my queries are often limited to entries assigned a specific fandom. In this case, I'm filtering by the fandom with FandomID value 19.
The relationship between Fanfics and Ships is similar to the other two, with a Ships table and a Fanfic_Ship junction table. While many fanfics have only one or even none assigned, others have two or even three or four. I ran across this issue because I first wanted to create a query pulling all of the fanfics in a specific fandom (19 being the FandomID) with a specific ship (ShipID being 49). This wasn't an issue when I wanted just any fics with that ship, and I could easily have left out the filtering by fandom first as only fanfics in that fandom would have that ship. (But you will see in a bit why I will need to filter by that fandom anyway.)
Below is that query:
Code: Select all
SELECT "Authors"."Author", "Fanfics"."Title", "Fanfics"."Rating", "Fanfics"."Wordcount", "Fanfics"."WebLink" FROM "Fanfic_Author", "Fanfic_Ships", "Fanfics", "Authors", "Fanfic_Fandom" WHERE "Fanfic_Author"."FanficID" = "Fanfics"."FanficID" AND "Fanfic_Author"."AuthorID" = "Authors"."AuthorID" AND "Fanfic_Fandom"."FanficID" = "Fanfics"."FanficID" AND "Fanfic_Fandom"."FandomID" = 19 AND "Fanfic_Ships"."FanficID" = "Fanfics"."FanficID" AND "Fanfic_Ships"."ShipID" = 49 ORDER BY UPPER ( "Authors"."Author" ) ASC, UPPER ( "Fanfics"."Title" ) ASC
However, when I wanted all the fics in that fandom that did not have that ship, I ran into issues; if a fic had, say, ShipID 49, 50, and 51 assigned to it, excluding the 49 meant that I got both entries for 50 and 51 listed, meaning the fanfic was listed twice in my results! Adding DISTINCT collapsed the entries so they only appeared once - but I still have the issue that if ShipID 49 is assigned to that fanfic, I don't want any of the entries of that fic to appear in my query. That fanfic should be excluded entirely from my results. How do I do that?
I did search but got completely confused as to how to set up a join that would work with the junction tables properly. I mean, this is five different tables being joined, three of them being junction tables, with filtering IN using one table and excluding entries using another! More complicated than anything I was running across in search results. I looked at this link but it didn't seem to quite fit my issue, as I'm trying to exclude rows in one table based on criteria in a junction table, not whether it does or doesn't have data at all in some table/column: viewtopic.php?f=61&t=43188&p=199007
As far as the issue with authors and fandoms, if I were to run a query for the same ship but had fanfics with multiple authors assigned that had that ship, is there any way to combine the data (in a "Author1 & Author2" or some other format) so that all the data for that fanfic can be listed a single time in the query, rather than duplicated, once for each author? The same goes for any fic that is in more than one fandom. This seems to be a close relative to the issue above (and shares the same background info on the database setup), so I figured I'd list it as well. I've already experienced multiple entries for a single fanfic in one query I do because the fanfic has two fandoms assigned, but as I have few of those that meet the other criteria of that query (which is filtered by a column in the Fanfics table, making it immensely simpler than the one I'm asking about above), it hasn't been an issue so far - but I can foresee it becoming so as I continue to enter data (I'm only to 538 entries as of today and this will end up in the thousands eventually).
I welcome any help or suggestions you can provide.
If it matters, the database has been split to HSQLDB, and all queries are already running the SQL commands directly because I wanted case-insensitive alphabetization in report results and it wouldn't do that otherwise.