MillPondFN Posted December 25, 2014 Share Posted December 25, 2014 I hope this isn't a stupid question. I have written a fairly large site using php and SQL. The site has multiple features (how tos, classifieds, photos, videos, ect) with each section's data housed in its own table. Now I would like to have one highlight from each section appear in boxes on the index page simutaniously. (basically returning one random item from each table when the page is loaded) I know how to call each table individually with sql = mysql_query, however, I fear that calling the database that many times over and over in one page load could be problematic if the site begins to get a lot of traffic. Am I correct in worring about overloading the server and is there a better way to do this? Quote Link to comment https://forums.phpfreaks.com/topic/293335-calling-multiple-sql-tables-at-once/ Share on other sites More sharing options...
NotionCommotion Posted December 25, 2014 Share Posted December 25, 2014 and is there a better way to do this? Yes! It is called JOIN. SELECT t1.this, t2.that, t3.andThis FROM table_1 AS t1 INNER JOIN table2 AS t2 ON t2.table_1_id LEFT OUTER JOIN table3 AS t3 ON t3.table_2_id=t2.id WHERE t1.bla=123; Quote Link to comment https://forums.phpfreaks.com/topic/293335-calling-multiple-sql-tables-at-once/#findComment-1500627 Share on other sites More sharing options...
hansford Posted December 25, 2014 Share Posted December 25, 2014 To elaborate on NotionCommon's comment: If your tables have common data then you can use INNER JOIN which will return the data that the selected tables have in common. If your tables have no common data or possibly some common data then you can use FULL OUTER JOIN which will return all rows from selected tables whether or not they share common data or not. Quote Link to comment https://forums.phpfreaks.com/topic/293335-calling-multiple-sql-tables-at-once/#findComment-1500636 Share on other sites More sharing options...
Jacques1 Posted December 26, 2014 Share Posted December 26, 2014 A join makes no sense in this context, because there's no relation between the items. The OP just wants one item from each table, and that's what a union does. To give concrete advice, however, we need to see the actual tables and the desired result set. Quote Link to comment https://forums.phpfreaks.com/topic/293335-calling-multiple-sql-tables-at-once/#findComment-1500641 Share on other sites More sharing options...
MillPondFN Posted December 26, 2014 Author Share Posted December 26, 2014 Thank you so much for responding. I read into both Join and Union and I don't think either of those will work for what I want to do. As I said before each section of the site is in a different table and each table has different column counts and data types and needs to be handled differently. For example the row for a classified will need to show price, location, and a link to the classifieds directory where a video will need to have the code to show the video. I was looking for something that would return all info from 1 random row in tables x,y, and z and then if from table x print this, if table y print this, ect..... Does that make sense? Quote Link to comment https://forums.phpfreaks.com/topic/293335-calling-multiple-sql-tables-at-once/#findComment-1500723 Share on other sites More sharing options...
NotionCommotion Posted December 26, 2014 Share Posted December 26, 2014 Without knowing your database schema, it is impossible to answer. I would expect, however, that you should just do three queries. Quote Link to comment https://forums.phpfreaks.com/topic/293335-calling-multiple-sql-tables-at-once/#findComment-1500729 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.