ernie Posted September 16, 2010 Share Posted September 16, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/213575-join-two-tables-with-different-fields-in-one-array/ Share on other sites More sharing options...
s0c0 Posted September 16, 2010 Share Posted September 16, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/213575-join-two-tables-with-different-fields-in-one-array/#findComment-1111758 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.