Cyclone
Cyclone

Reputation: 15269

Select data from three tables

First of all, those tables doesn't have any ties, so I can not use the JOIN.

I got three tables with another data, and I need to display all of the data from those three tables in one query with one result.

For example I got:

Hot News table that contains; title, text, icon rows.

Bad News table that contains; title, text, icon rows.

Good News table that contains; title, text, icon rows.

And I need to withdraw all news in one query. Is it possible?

Upvotes: 0

Views: 185

Answers (4)

Moo-Juice
Moo-Juice

Reputation: 38825

If I read your question right:

SELECT title, text, icon, 'News' AS type
FROM HotNews
UNION ALL
SELECT title, text, icon, 'Bad' AS type
FROM BadNews
UNION ALL
SELECT title, text, icon, 'Good' AS type
FROM GoodNews

By including type this means that whatever you are using to render this data to the user, can tell what kind of news it is.

Upvotes: 1

Mike Christensen
Mike Christensen

Reputation: 91590

SELECT Title, Text Icon From HotNews
UNION ALL
SELECT Title, Text Icon From BadNews
UNION ALL
SELECT Title, Text Icon From GoodNews

Upvotes: 1

Matthew
Matthew

Reputation: 25743

Check out the UNION operator http://dev.mysql.com/doc/refman/5.0/en/union.html

Upvotes: 1

Mark Byers
Mark Byers

Reputation: 837996

Use UNION:

SELECT title, text, icon FROM `Hot News`
UNION
SELECT title, text, icon FROM `Bad News`
UNION
SELECT title, text, icon FROM `Good News`

Note that UNION removes duplicates. If you want to keep duplicates, use UNION ALL instead.

Upvotes: 3

Related Questions