I've embarked on a project which is encouraging me to expand my knowledge of SQL. I've already learned quite a bit, but I've gotten myself to a point where I can see a problem, but I don't know enough to properly research a solution. I've hit SO, Google, and the MySQL docs, but either I'm asking the wrong questions or I don't know how to ask the right ones. My data is structured with three primary tables, one of which has M:N relationships with the other two. I'm storing those relationships in another pair of tables, as I'm under the impression that's the "best way":
books (book_id INT PRIMARY KEY, book_title VARCHAR)
authors (author_id INT PRIMARY KEY, author_name VARCHAR)
subjects (subject_id INT PRIMARY KEY, subject_name VARCHAR)
book_authors (book_id INT, author_id INT)
book_subjects (book_id INT, subject_id INT)
(The first three tables actually have more than two columns, but they aren't relevant.)
Edit:
Obviously, I suck at asking clear questions, but then I already knew that. :-)
The issue I'm trying to solve is how to most effectively/efficiently get the data from the database into my app. Once I have it there, I can rearrange it however I need, and I trust I can do that regardless of how the data is "shaped" coming out of the database. It would be trivial to do with five separate SELECT * FROM …
statements and it would be trivial to do with with the cross-product "frankenjoin" I had posted earlier. However, I know enough about SQL to spot that the former robs the database engine of the opportunity to do its job. I don't know enough about SQL to say whether the latter is equally bad.
So how about this: instead of asking what's wrong with the solution I came up with, what would be your solution? If you had data related this way, how would you select it out of the database? (And for what reasons?) Would you perhaps even arrange the tables differently?