|
Posted by Steve on 11/16/10 11:31
> I would like to select all the rows from both tables in a single SELECT
> statement so when I output them they will be integrated. This is not
> the same as a table join as their are no keys and I want the rows from
> both to remain seperate. Can this be done? Could someone please post an
> example SQL statement demonstrating this if so?
You need a UNION statement or the equivalent. The syntax will depend on
what database server you are using. I'll assume MySQL, and since UNION
is only available from 4.0.0 onwards I'll assume you have that version.
The main requirement is that the columns of both tables have the same
names and the same types. You haven't given any DDL for the tables so
I'll make some up...
CREATE TABLE CommunityNews ( cnid INT, newsdate DATE, newstext
VARCHAR(1000) )
CREATE TABLE PressNews ( pnid INT, newsdate DATE, newstext
VARCHAR(1000) )
(SELECT newsdate, newstext, "1" AS source
FROM CommunityNews
WHERE newsdate >= '"20051107")
UNION
(SELECT newsdate, newstext, "2" AS source
FROM PressNews
WHERE newsdate >= "20051107")
ORDER BY newsdate DESC
Your result is now a mixture of rows from both tables. You can tell
which table a row came from by looking at the "source" column.
---
Steve
Navigation:
[Reply to this message]
|