Можеш да използваш UNION като по този начин можеш да обединиш в един резултат, резултатите от заявки съм различни таблици. Изискването е колоните в резултата да бъдат с еднакви имена.
[sql]
CREATE TABLE `news` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
`content` text NOT NULL,
`posted_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
);
CREATE TABLE `articles` LIKE `news`;
CREATE TABLE `appearances` LIKE `news`;
insert into news values (null, 'news title', 'news content', null);
insert into articles values (null, 'article title', 'article content', null);
insert into appearances values (null, 'appearance title', 'appearance content', null);
mysql> select * from news;
+----+------------+--------------+---------------------+
| id | title | content | posted_at |
+----+------------+--------------+---------------------+
| 1 | news title | news content | 2013-05-11 12:22:03 |
+----+------------+--------------+---------------------+
1 row in set (0.00 sec)
mysql> select * from articles;
+----+---------------+-----------------+---------------------+
| id | title | content | posted_at |
+----+---------------+-----------------+---------------------+
| 1 | article title | article content | 2013-05-11 12:22:27 |
+----+---------------+-----------------+---------------------+
1 row in set (0.01 sec)
mysql> select * from appearances;
+----+------------------+--------------------+---------------------+
| id | title | content | posted_at |
+----+------------------+--------------------+---------------------+
| 1 | appearance title | appearance content | 2013-05-11 12:21:18 |
+----+------------------+--------------------+---------------------+
1 row in set (0.00 sec)
[/sql]
и сега всички заедно:
[sql]
mysql> (select * from appearances) union (select * from news) union (select * from articles) order by posted_at;
+----+------------------+--------------------+---------------------+
| id | title | content | posted_at |
+----+------------------+--------------------+---------------------+
| 1 | appearance title | appearance content | 2013-05-11 12:21:18 |
| 1 | news title | news content | 2013-05-11 12:22:03 |
| 1 | article title | article content | 2013-05-11 12:22:27 |
+----+------------------+--------------------+---------------------+
[/sql]
До тук имаш резултат, който можеш да покажеш лесно пред потребителя. Ако трябва да направиш линкове обаче към тези новини и статии, трябва по някакъв начин да знаеш всеки един ред от резултата, за какъв тип информация се отнася. За целта, към всяка заявка, можеш да добавиш още една колона, с предварително зададена от теб стойност:
[sql]
mysql> (select *, 'appearance' as data_type from appearances) union (select *, 'news' as data_type from news) union (select *, 'article' as data_type from articles) order by posted_at;
+----+------------------+--------------------+---------------------+------------+
| id | title | content | posted_at | data_type |
+----+------------------+--------------------+---------------------+------------+
| 1 | appearance title | appearance content | 2013-05-11 12:21:18 | appearance |
| 1 | news title | news content | 2013-05-11 12:22:03 | news |
| 1 | article title | article content | 2013-05-11 12:22:27 | article |
+----+------------------+--------------------+---------------------+------------+
[/sql]
Ако колоните в различните таблици са с различни имена, можеш да използваш alias:
[sql]
mysql> (select app_id as id, app_title as title, app_content as content, app_posted_at as posted_at from appearances) union (select news_id as id, news_title as title, news_content as content, news_posted_at as posted_at from news) union (select aid as id, atitle as ttle, acontent as content, a_posted_at as posted_at from articles) order by posted_at;
[/sql]