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
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho