Jump to content

writing smart multi-table queries


bcamp1973

Recommended Posts

i'm building a simple website CMS.  on loading a page the database is queried for the page details. the query hits 3 tables..."pages","images" and "links". Can i write a single query to grab all the needed data from all 3 tables to build the page?  The "images" table contains one or more images for each page, and teh "links" table contains one or more links for each page. The tables look like so...

pages
-------------
id
page_title

page_pictures
-------------
id
picture_id
page_id

links
-------------
id
page_id
link

I'm using MySQL 4.1.15. So far i've only been able to get all this data using 3 separate queries.  Not sure if that's considered "messy" or if it's really the only way to go?
Link to comment
Share on other sites

Well, if you need all the pictures & links for a given page, you'd need two statements right there (I consider UNION still 2 statements, but you could get away with a single  UNION statement).  You'd have to JOIN in the pages table to be sure, but that's still not any less "messy", as you put it.
Link to comment
Share on other sites

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.