2010-12-04 68 views
0

我基本上有三个不同类的项目,我想在用户墙上显示:评级,评论和更新。这三个是完全不同的实体,但因为它们都可以出现在用户墙上,所以我只称它们为“wallitem”。全部都有一个timestamp属性,它代表了它们被创建的日期。MySQL:在一个查询中合并不同实体

我想使用户能够浏览通过时间戳排序的wallitems。例如:最后10个wallitems。或者wallitems 20到30.是否有MySQL查询给了我最后的10个“wallitems”,尽管所有不同的实体都有不同的列?

我可以想象得到一个项目清单,其中每个项目具有所有不同实体的所有属性,一个额外的属性定义类型(例如“评级”),如果它实际上是一个评级,所有其他属性只是null。我很想在我的PHP代码中使用这样的dicationary:

foreach ($wallItemArray as $item) { 
    if ($item['type'] == "rating") { 
    $rating = $item; // use it as normal "rating"-entity 
    } else if ($item['type'] == "comment") { 
    $comment = $item; // use it as normal "comment" 
    } 
// and so on for all different entities that could represent a wallitem in this context 
} 
+0

也许工会选择? – thejh 2010-12-04 22:27:27

回答

2

喜欢的东西

SELECT 'rating' AS type, value AS r_val, NULL AS c_val 
FROM Rating 

UNION 

SELECT 'comment' AS type, NULL AS r_val, comment_text AS c_val 
FROM Comment 

将让你开始

相关问题