2015-12-17 78 views
9

我知道以前也有類似的問題,但沒有一個具有相同的條件,他們的答案對這種情況不起作用。從每次對話中獲取最後一條消息

包含的消息的表看起來像這樣:

id | owner_id | recipient_id | content  | created 
1 |  1 |   2 | Hello  | 2015-12-08 20:00 
2 |  2 |   1 | Hey   | 2015-12-08 20:10 
3 |  3 |   1 | You there? | 2015-12-08 21:00 
4 |  1 |   3 | Yes   | 2015-12-08 21:15 
5 |  4 |   1 | Hey buddy | 2015-12-08 22:00 

而且我們說,我查詢用戶ID 1的談話,預期的結果是:

id | owner_id | recipient_id | content  | created 
5 |  4 |   1 | Hey buddy | 2015-12-08 22:00 
4 |  1 |   3 | Yes   | 2015-12-08 21:15 
2 |  2 |   1 | Hey   | 2015-12-08 20:10 

我試過很多的組合使用,聯合和子查詢,但沒有一個給出了預期的結果。

我正在尋找MySQL中的答案,但是這將用於CakePHP 2開發的項目中,所以如果有一個Cake特定的方法來做到這一點,那就太棒了。

這是我嘗試過的一個查詢,但它不工作。我相信甚至沒有接近我所需要的。

SELECT 
    IF (owner_id = 1, recipient_id, owner_id) AS Recipient, 

    (
     SELECT 
      content 
     FROM 
      messages 

     WHERE 
      (owner_id = 1 AND recipient_id = Recipient ) 
     OR 
      (owner_id = Recipient AND recipient_id = 1) 

     ORDER BY 
      created DESC 

     LIMIT 1 
    ) 
FROM 
    messages 

WHERE 
    owner_id = 1 
OR 
    recipient_id = 1 

GROUP BY 
    Recipient; 
+2

表中是否有唯一鍵? – Timekiller

+0

是的,我更新了這個問題(拼錯了幾個ID)。 – Camilo

+0

您是否可以包含您在問題中嘗試過的SQL查詢? – summea

回答

5
select t.* 
    from 
     t 
     join 
     (select user, max(created) m 
      from 
       (
       (select id, recipient_id user, created 
        from t 
        where owner_id=1) 
       union 
       (select id, owner_id user, created 
        from t 
        where recipient_id=1) 
       ) t1 
      group by user) t2 
    on ((owner_id=1 and recipient_id=user) or 
     (owner_id=user and recipient_id=1)) and 
     (created = m) 
    order by created desc 

example on sqlfiddle

+0

這個查詢返回預期的結果,非常感謝你!儘管我仍在試圖弄清楚它是如何工作的。 – Camilo

+1

不客氣!我可以寫一些爆炸,如果你想 – splash58

+0

這將是偉大的@ splash58 – Camilo

0

這應該做的伎倆:

$joins = array(
    array('table' => 'conversations', 
     'alias' => 'Conversation2', 
     'type' => 'LEFT', 
     'conditions' => array(
      'Conversation.id < Conversation2.id', 
      'Conversation.owner_id = Conversation2.owner_id', 
     ) 
    ), 
    array('table' => 'conversations', 
     'alias' => 'Conversation3', 
     'type' => 'LEFT', 
     'conditions' => array(
      'Conversation.id < Conversation3.id', 
      'Conversation.recepient_id = Conversation3.recepient_id', 
     ) 
    ) 

); 

$conditions = array(
    'OR' => array(
     array(
      'Conversation2.id'=>null, 
      'Conversation.owner_id' => $ownerId 
     ), 
     array(
      'Conversation3.id'=>null, 
      'Conversation.recipient_id' => $ownerId 
     ), 
    ) 
); 

$order = array('Conversation.created'=>'DESC'); 

$lastConversations=$this->Conversation->find('all',compact('conditions','joins','order')); 

提供該表的名稱是conversations和模型的名稱是Conversation。它基於Retrieving the last record in each group接受的答案中描述的技術。

+0

這一個似乎是接近。目前唯一的問題是結果不是最近訂購的。試圖添加「訂單」參數,但它沒有解決問題。 – Camilo

+0

@Camilo我已經添加了ORDER BY子句。 –

+0

謝謝。我試過了,問題仍然存在。還注意到分組不能按預期工作。我會堅持SQL答案,謝謝! – Camilo

相關問題