Jump to content

Join two tables with different fields in one array


Recommended Posts

Hello,

 

I'm new to PHP and this forum, and so far this forum popped up on lots of google searches concerning PHP so I figured I'd register, because I am in need of some help. I'll try and explain it correctly.

Problem is as follows:

 

I need to perform a SELECT * on two tables and sort the results in a html table. The amount of fields in the tables is different, and so are the names of the fields. Except one field, called 'date_posted'. It's on this field I would like to sort my results.

 

First query is this: "SELECT cl_photos.*, cl_albums.* FROM cl_photos INNER JOIN cl_albums

ON cl_photos.album_id = cl_albums.album_id"

 

Second query is this: "SELECT cl_blog.*, cl_categories.* FROM cl_blog INNER JOIN cl_categories

ON cl_blog.cat_id = cl_categories.cat_id WHERE cl_blog.cat_id='$id'"

 

The results of those two queries give me everything under cl_photos and cl_blog, every result attached to the album or category it belongs to. What I want to do is get the results of those two queries in one array and sort it by 'date_posted', which is a field cl_photos and cl_blog have in common.

Does anyone know how to do this? Is it possible? I think I need more JOINs but I'm far from sure.

 

Thanks in advance.

You could attempt using a UNION.  A union combines to querries and returns them in the same result set.  However, the columns must be the same.  You could fake this by creating aliases from the columns. You know like "column AS columnAlias.

 

http://dev.mysql.com/doc/refman/5.0/en/union.html

 

Your other option is programatically to merge the arrays into one with whatever language you're using.

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.