Solved

MySQL query with multiple joins on 7 tables

Posted on 2008-10-29
3
194 Views
Last Modified: 2011-10-19
Hello,

I need help with a query involving 7 tables. I've attached a visual schema that I hope will help.
What I want to do is select all of the documents and display their associated data in each table.

1. Show all the fields in all the records in docs
2. Match docs.doc_cat->category.cat_id to show the cat_name field in category
3. Match docs.doc_origin->origin.origin_id to show the origin field in origin
4. Match docs.doc_id=mapchapters.doc_id AND chapters.chapter_id=mapchapters.chapid to show the chapter field in the chapters table
5. Match docs.doc_id=mapcriteria.doc_id AND criteria.criteria_id=map_criteria.criteria_id to show the criteria field in the criteria table

I was doing fine joning the origin, docs, and category tables,

but when it came to steps 4 and 5 I hit a wall.

each document has 1 category and 1 origin but many criteria and many chapters
document->chapters=many:many
document->criteria=many:many

I hope that I have provided all of the information needed to answer this query, but if not please let me know and I will get back to you right away.
Thanks,
Audg
SELECT * FROM docs AS d1,

LEFT JOIN category AS c1 ON d1.doc_cat=c1.cat_id 

LEFT JOIN origin AS o1 ON d1.doc_origin=o1.origin_id

Open in new window

archiveschema.jpg
0
Comment
Question by:aberns
  • 2
3 Comments
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 500 total points
ID: 22834349
You were on the right track ... I think this is your answer.

SELECT d1.*, c1.cat_name, o1.origin, ch.chapter,  cr.criteria

FROM docs AS d1

LEFT JOIN category AS c1 ON d1.doc_cat=c1.cat_id 

LEFT JOIN origin AS o1 ON d1.doc_origin=o1.origin_id

Left Join mapchapters mc on d1.doc_id = mc.doc_id

Left Join chapters ch on mc.chapter_id = ch.chapter_id

left join map_criteria mcr on d1.doc_id = mcr.doc_id

left join criteria cr on cr.criteria_id = mcr.criteria_id

Open in new window

0
 

Author Comment

by:aberns
ID: 22835666
Awesome! It worked! Thanks. I kept thinking I had to define docs with different aliases since I was joining it to multiple tables. Also, is the AS optional? i.e. you have
Join mapchapters mc
but
Join origin AS 01
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 22835699
Yes, I'm pretty sure AS is optional.  I know it is in MS SQL Server ...
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Update in Sql 7 30
poor performance from  MySQL stored procedure 6 23
Download OAB 0x8004010f An object cannot be found 12 46
Update from TABLE-A to TABLE-B 5 39
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now