Jump to content

Archived

This topic is now archived and is closed to further replies.

bcamp1973

writing smart multi-table queries

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?

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

×

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.