Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
This week, we're updating our course layouts and descriptions. Presentation and materials always gently change over time, but just occasionally there's a need to make a step change to clear out some of the old and roll in the new. That's now happening - but over a long and complex site it's not instant and you'll see sections of the site changing up to and including 19th September.

See also [here] for status update
 
Querying multiple table

Posted by jennww (jennww), 30 November 2005
Hi all,

I am new to php and starting my first project, which is an application to follow our projects. I have normalise my database which create 5 tables and now need to query the information for each project.
I have read up on two of your articles about joins in queries and three way join (this allowed me to create my project list which I have also got to link through to a second page). However I have 5 tables to link or query to show each projects full details.  Please can you point me in the right direction.

Thanks

Posted by admin (Graham Ellis), 30 November 2005
You can nest multiple joins or write a whole  list of table names - once you've joined three just extend the principle for the fourth and fifth.

For example, here's a join we regularly use on our 5-table book database:

select isbn, title, favourite, retired, replacedby, pubdate, minlevel, maxlevel, fullname, asynopsis, jpeg, edition, pubname, synopsis, bid, subject  into outfile "/Library/WebServer/live_html/lib.wellho/bdata" fields enclosed by '"' from b_btab, b_atab, b_pivot, b_stab, b_ptab where b_btab.biid = b_pivot.biid and b_atab.aiid = b_pivot.aiid and b_stab.biid = b_btab.biid and b_ptab.piid = b_btab.piid order by isbn;


It's old .. in hindsight, I whould have used a "join" syntax .... but it's still a pointer to the sort of way it's done/

Posted by jennww (jennww), 1 December 2005
Thank you for your reply.

The three way join is;
SELECT title, description, ref_name, ref_last, lead_name, lead_last, project_id FROM (track_pro left join track_referees on  track_pro.ref_id =  track_referees.ref_id) left join track_leads on track_leads.lead_id = track_pro.lead_id

My problem is how to you add the next join, I have the first in () and then the second not. To add the next I have tried;
SELECT title, catagory, ref_name, ref_last, ref_email, lead_name, lead_last, description, fund, anticipated, considerations, billing_date, date_submit, date_complete FROM (track_pro left join track_referees on track_pro.ref_id = track_referees.ref_id) AND (track_pro left join track_categorys on track_pro.category_id = track_categorys.category_id) left join track_leads on track_leads.lead_id = track_pro.lead_id

I get '#1064 - You have an error in your SQL syntax'

I have also tried without the AND and with , and without anything. also tried put the second () inside the first, but it does not like it. This is what I am stuggling with.

Posted by admin (Graham Ellis), 1 December 2005
Try the form

select f1,f2,f3,f4 from ((first left join second on fid = sfid) left join third on sid = tid) left join fourth on sidf = fid;

That line gave me "table does not exist" when I tried it, so I think it's passed all the syntax tests  

Posted by jennww (jennww), 2 December 2005
Many many thanks for you help. I have it up and running. I have even extended it to include info from another table.

Thanks Again



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho