I’m using MS Access with a MySQL database. One table has records which represent ‘documents’:
Table: doc
Primary Key: doc_id
These records can be linked together via a link table:
Table: link
Primary Key: link_id
Foreign Key: doc_id_A
Foreign Key: doc_id_B
Thus the records may be linked in a chain, eg Doc A linked to Doc B, which is linked to Doc C, etc, and also, Doc A may be linked to any number of other documents.
In reality, the ‘family’ of inter-related documents wouldn’t exceed 20 records.
I’m looking for an efficient MySQL proc, or VBA function – or a query – to find all the members of a ‘family’ for one specified record.
Any suggestions would be most welcome!
So the link table gives a self join to doc, you absolutely need a doc to be able to:
So your link table could have 10 separate links 1-1, 1-1, 1-1, 1-2, 1-2, 1-2, 2-1, 2-1, 2-1, 2-2 etc. with just 2 docs in the ‘family’.
I expect when you look at this list you will probably think that you don’t need most of them, a lot of the inefficiency in your solution might be coming from this unnecessary flexibility. My favoured suggestion would be to start with a strict hierarchy and build minimally from there.
But here is my answer anyway, it’s tested and working in Access-2010 and local tables. ADODB should work just as well with linked tables.
Duplicated results and queries are avoided by excluding items already found at the server, and as the link is unidirectional I’ve used a
UNIONquery to look both ways at once. At most it’ll doMaxInFamilyround trips to the server.