开发者

Timeline from 2 related tables

Let's say I have a screenshots table and a replies table. So: every screenshot can have multiple replies (one-to-many). Now, I want to create a combined timeline of the two, but in reality, they're pretty unrelated (in structure).

How can I select data from both tables, ordering by their publish time, descending; when for example, I can have a post, a few comments, then another post; because that would be how the timeline happened?

Normally selecting from both combines the tables; but I don't want that to happen. On that subject, I also need the tables to be distinguishable. Here's the structure for the tables...

--
-- Table structure for table `screenshots`
--

CREATE TABLE IF NOT EXISTS `screenshots` (
  `id` int(11) NOT NULL auto_increment,
  `user` int(11) NOT NULL,
  `description` text NOT NULL,
  `time` timestamp NOT NULL default CURRENT_TIMESTAMP,
  `ext` varchar(4) NOT NULL default 'png',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=14 ;

-- --------------------------------------------------------

--
-- Table structure for table `screenshot_replies`
--

CREATE TABLE IF NOT EXISTS `screenshot_replies` (
  `id` int(11) NOT NULL auto_increment,
  `user` int(11) NOT NULL,
  `parent` int(11) NOT NULL,
  `time` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  `text` text NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=27 ;

Note: I realize this may be开发者_JAVA百科 a duplicate of this question but I didn't find the solution there working for me.

Thanks in advance! :)


You should use UNION in this case:

(SELECT id, time, 'screenshots' as tableName FROM screenshots)
UNION
(SELECT id, time, 'replies' as tableName FROM screenshot_replies)
ORDER BY time ASC

You can get the tablename of a field by using the mysql_tablename function in php
You can indicate the tablename as a column in the result set


Whenever I come across a problem like this, where you're finding it impossible to do something because you can't built a query which will extract the data, I have to start wondering do you have the right data model?

Start with what data you want to extract, and then build a model which allows that, not the reverse, and you'll find it much easiler in the long term, and also probably faster performance and quite often better flexibility.

Looking at those tables, I'd have to ask why have two of them? What's wrong with:

CREATE TABLE IF NOT EXISTS `screenshots` (
  `id` int(11) NOT NULL auto_increment,
  `user` int(11) NOT NULL,
  `description` text NOT NULL,
  `time` timestamp NOT NULL default CURRENT_TIMESTAMP,
  `ext` varchar(4) default 'png',
  `parent` int(11),
  `text` text,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1;

That would allow you to do what you want, easily telling if it's a screen shot (if ext is set), a reply (if parent is set) or ... since the model now allows it ... a screenshot which is a reply!!!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜