2010-04-09 77 views
1

我有兩個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

看起來像一門功課,我... – 2010-04-09 10:00:24

回答

3

試試這個:

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