I have a database with several tables, 5 of which are dedicated to specific publication types. Each of these 5 have a one->many relationship with a status table and a people table. All of these tables are tied together using a unique “pubid”. I have a view which includes the pubid (for all 5 types), along with their associated keywords. When a user does a keyword search and the results span across more than 1 of those 5 publication type tables I am really not sure how to handle it.
If there was only a single publication type (so just one table that had a 1->many) it would be very easy to accomplish with a nested join, something like:
SELECT * FROM articles
INNER JOIN status ON articles.spubid = status.spubid
INNER JOIN people ON articles.spubid = people.spubid
WHERE people.saffil = 'ABC' ORDER BY people.iorder, articles.spubid;
In that example ‘articles’ is one of the 5 tables I mentioned that has a 1->many relationship. Lets say that the keyword search brings back results that include articles, books and papers. How can I achieve this same end with that many different tables? If I were to figure out how to use a JOIN in that case the Cartesian product would be so large that I think the overhead to parse it out into a usable format would be too high. What are my other options in this case?
Why are they in separate tables? Are the columns that different? And what columns do you want to return (never ever use select * in production especially with joins), are they different between the different types in your query?
If you can get the columns to be the same I suggest you use UNION ALL. Even if the columns are different by supplying all that you need in each part of the union statement (giving the value of null for those columns that set of tables doesn’t have), you can still get what you want. Simplified code follows: