Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/293335-calling-multiple-sql-tables-at-once/
Share on other sites

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;

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.

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.

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.