You are here: Re: Mix two tables in a single SELECT statement - not a join « PHP Programming Language « IT news, forums, messages
Re: Mix two tables in a single SELECT statement - not a join

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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация