954,580 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to you select from multiple tables

On a project I am doing I am making a Feed Flow of recent updates. Problem is I cant find out to overcome the following.

There are two tables, shouts and journals. what I want to do is pull records from them both and print them out with the newest at the top and the oldest at the bottom.

I have tried every way and cant find how to do it, but heres the logic of what I am trying.

The two tables fields are

shouts = id, content, date, user
journals = id, content, title, date, user

SELECT * FROM shouts, journals ORDER BY id DESC;

While( $row = above statement )
{

if its a shout
{

print "Shout: $row['content']";

} else {

print " Journal: $row['title']
$row['content']";

}

The final result of the flow should be something like

SHOUT
SHOUT
JOURNAL
SHOUT
JOURNAL

ect ect but at the moment all I can seem to get working is all journal or all shout


Any help Highly appreciated.


}

Mi-Dia
Newbie Poster
12 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 
SELECT `id`,`content`,`date`,`user`, `title`, "j" AS `type`
FROM `journals`
UNION
SELECT `id`,`content`,`date`,`user`, NULL AS `title`, "s" AS `type`
FROM `shouts`
ORDER BY id DESC


The NULL as title is a must as UNION requires the same number of fields (and the same datatype). I've included `type` so when you loop through the resultset, you could easily identify which is which and e.g. provide some conditional formatting/colour scheme etc.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: