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";

} else {

print " Journal: $row <br> $row";

}

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.


}

Member Avatar for diafol
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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.