查找表X、Y和Z中按创建日期排序的最新N项


Do a find on the latest N items from tables X, Y, and Z sorted by date created?

我想使用CakePHP查找3个不同的数据库表。

收藏夹评论列表

每个表都有自己的模式,包括id、created、modified和其他一些字段。我想对所有3个按创建日期排序的组合进行查找。所以本质上。。。

[0] => array(
    [Favorite]
),
[1] => array(
    [Comment]
),
[2] => array(
    [Favorite]
),
[3] => array(
    [Listing]
)

假设这些是按照MySQL中的DATETIME创建字段排序的。

像这样使用UNION运算符:

SELECT * FROM (
        SELECT id, created, modified, "Favorite" as ST FROM Favorite
  UNION SELECT id, created, modified, "Comment"  as ST FROM Comment
  UNION SELECT id, created, modified, "Listing"  as ST FROM Listing
) 
WHERE ....
LIMIT ...
;