Merge Two Tables Data

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Merge Two Tables Data

Post by Dale »

Hey,

I've Google'd about and tried many different examples of what I need and none seemed to work, soooo hopefully you'll be able to help me. Basically I have a table that "logs" user activity and also a table that is for "wall posts" and I basically want to show both of them together in one list.

Here's the table's structure...
log
id, userid, loginfo, timeadded

wallpost
id, userid, thepost, timeadded

Now I want to have it merge both together and ordered using the timeadded value. Now I've tried doing it with JOIN, LEFT JOIN and various other methods (including having two seperate queries and results that are added together as a seperate array - but that didn't work either).

Any ideas?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Merge Two Tables Data

Post by McInfo »

Try UNION.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Merge Two Tables Data

Post by califdon »

As McInfo said, if you want to collect ALL records of BOTH tables, sorted by whatever, UNION is the kind of query you need to use. But if you are trying to MATCH UP records with the same userid, or something, and display both loginfo and thepost, then you have an entirely different situation. In that case, you have to decide what to do for each possible situation, such as loginfo, but no thepost data, or the reverse, and whether you want to group by userid or just sort by timestamp.
Post Reply