我基本上想要在用户墙上显示三类不同的项目:评级、评论和更新。这三个是完全不同的实体,但因为它们都可以出现在用户墙上,所以我将它们称为“wallitem”。它们都有一个时间戳属性,代表它们的创建日期。
我想让用户能够按时间戳顺序翻阅墙项目。例如:最后 10 个墙项目。或者 wallitems 20 到 30。是否有一个 MySQL 查询可以为我提供最后 10 个“wallitems”,即使所有不同的实体都有不同的列?
我可以想象,获取一个项目列表,其中每个项目都具有所有不同实体的所有属性,定义类型的附加属性(例如“评级”),如果它实际上是评级,则所有其他属性只是空。我很想在我的 php 代码中使用这样的指示符:
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
}
最佳答案
类似于
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
让你开始
关于MySQL:在一个查询中合并不同的实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4356153/