如何基于公共列合并两个表并按日期对结果进行排序

时间:2010-04-09 09:34:33

标签: mysql

我有两个mysql表,我想根据公共列 rev_id 合并这两个表的结果。合并后的结果应按两个表的日期排序。

请帮帮我。

CREATE TABLE  `reply` (
  `id` int(3) NOT NULL auto_increment,
  `name` varchar(25) NOT NULL default '',
  `member_id` varchar(45) NOT NULL,
  `rev_id` int(3) NOT NULL default '0',
  `description` text,
  `post_date` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  `flag` char(2) NOT NULL default 'N',
  PRIMARY KEY  (`id`),
  KEY `member_id` (`member_id`)
) ENGINE=MyISAM;

CREATE TABLE `comment` (
  `com_id` int(8) NOT NULL auto_increment,
  `rev_id` int(5) NOT NULL default '0',
  `member_id` varchar(50) NOT NULL,
  `comm_desc` text NOT NULL,
  `date_created` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  PRIMARY KEY  (`com_id`),
  KEY `member_id` (`member_id`)
) ENGINE=MyISAM;

1 个答案:

答案 0 :(得分:3)

试试这个:

SELECT * FROM reply LEFT JOIN comment ON reply.rev_id = comment.rev_id ORDER BY reply.post_date, comment.date_created