Querying multiple table
Posted by jennww (jennww), 30 November 2005Hi 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.
Posted by admin (Graham Ellis), 30 November 2005You 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 2005Thank 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 2005Try 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 2005Many many thanks for you help. I have it up and running. I have even extended it to include info from another table.
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: firstname.lastname@example.org • WEB: http://www.wellho.net • SKYPE: wellho