如何通过create_time一起从多个表中输出数据

时间:2011-07-20 10:11:13

标签: mysql sql-order-by

使用'order by'子句在一个表中输出数据非常方便但如何按顺序从不同的表输出数据?

请参阅,我有表article post photo comment来保存用户生成数据,我想在主页中按时间顺序显示这些数据。

怎么做?

抱歉,我是绿手,我不知道回答这个问题需要一个表定义。 这是表定义之一:

| tbl_post | CREATE TABLE `tbl_post` (
  `post_id` int(11) NOT NULL AUTO_INCREMENT,
  `bench` int(11) NOT NULL,
  `author` int(7) NOT NULL,
  `tag` varchar(45) DEFAULT NULL,
  `title` varchar(45) NOT NULL,
  `content` text NOT NULL,
  `create_time` datetime NOT NULL,
  PRIMARY KEY (`post_id`),
  KEY `post_id` (`post_id`),
  KEY `author` (`author`),
  KEY `author_2` (`author`),
  KEY `bench` (`bench`),
  CONSTRAINT `tbl_post_ibfk_1` FOREIGN KEY (`author`) REFERENCES `tbl_user` (`id
`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `tbl_post_ibfk_2` FOREIGN KEY (`bench`) REFERENCES `tbl_bench` (`be
nch_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8 |

tbl_article

| tbl_article | CREATE TABLE `tbl_text` (
  `article_id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(50) NOT NULL,
  `content` text NOT NULL,
  `tag` varchar(45) DEFAULT NULL,
  `author` int(7) NOT NULL,
  `create_time` datetime DEFAULT NULL,
  PRIMARY KEY (`article_id`),
  KEY `author` (`author`),
  KEY `create_time` (`create_time`),
  CONSTRAINT `tbl_text_ibfk_1` FOREIGN KEY (`author`) REFERENCES `tbl_user` (`id
`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |

tbl_photo

| tbl_photo | CREATE TABLE `tbl_photo` (
  `photo_id` int(11) NOT NULL AUTO_INCREMENT,
  `author` int(7) NOT NULL,
  `description` varchar(120) DEFAULT NULL,
  `tag` varchar(45) DEFAULT NULL,
  `name` varchar(45) DEFAULT NULL,
  `file_name` char(132) DEFAULT NULL,
  PRIMARY KEY (`photo_id`),
  KEY `author` (`author`),
  CONSTRAINT `tbl_photo_ibfk_1` FOREIGN KEY (`author`) REFERENCES `tbl_user` (`i
d`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |

tbl_post_comment

| tbl_post_comment | CREATE TABLE `tbl_post_comment` (
  `post_comment_id` int(11) NOT NULL AUTO_INCREMENT,
  `post` int(11) NOT NULL,
  `author` int(7) NOT NULL,
  `content` varchar(1000) NOT NULL,
  `parent` int(11) DEFAULT NULL,
  `create_time` datetime NOT NULL,
  PRIMARY KEY (`post_comment_id`),
  KEY `post` (`post`),
  KEY `author` (`author`),
  CONSTRAINT `tbl_post_comment_ibfk_1` FOREIGN KEY (`post`) REFERENCES `tbl_post
` (`post_id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `tbl_post_comment_ibfk_2` FOREIGN KEY (`author`) REFERENCES `tbl_us
er` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 |

1 个答案:

答案 0 :(得分:0)

只需确保所有子查询具有相同数量的字段且顺序相同:

SELECT 'post' AS type
     , p.post_id AS id
     , p.author
     , p.create_time
FROM tbl_post AS p
WHERE author = 42

UNION ALL

SELECT 'article' AS type
     , a.article_id AS id
     , a.author
     , a.create_time
FROM tbl_article AS a
WHERE author = 42

UNION ALL

SELECT 'photo' AS type
     , ph.photo_id AS id
     , ph.author
     , ph.create_time
FROM tbl_photo AS ph
WHERE author = 42

UNION ALL

SELECT 'comment' AS type
     , c.comment_id AS id
     , c.author
     , c.create_time
FROM tbl_comment AS c
WHERE author = 42

ORDER BY create_time