2010-12-04 46 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 

將讓你開始