2014-10-16 118 views
0

我正在创建一个小的消息平台,我有三个表:MySQL查询以笛卡尔积

`msg_conversations` 
+-------+---------------------+------+-----+---------+----------------+ 
| Field | Type    | Null | Key | Default | Extra   | 
+-------+---------------------+------+-----+---------+----------------+ 
| id | int(10) unsigned | NO | PRI | NULL | auto_increment | 
+-------+---------------------+------+-----+---------+----------------+ 

`msg_participants` 
+------------+---------------------+------+-----+---------+----------------+ 
| Field  | Type    | Null | Key | Default | Extra   | 
+------------+---------------------+------+-----+---------+----------------+ 
| id   | int(10) unsigned | NO | PRI | NULL | auto_increment | 
| convo_id | int(10) unsigned | NO | MUL | NULL |    | 
| prof_id | int(10) unsigned | NO | MUL | NULL |    | 
| prof_type | tinyint(3) unsigned | NO |  | NULL |    | 
| is_visible | tinyint(3) unsigned | NO |  | NULL |    | 
+------------+---------------------+------+-----+---------+----------------+ 

`msg_messages` 
+----------------+---------------------+------+-----+---------+----------------+ 
| Field   | Type    | Null | Key | Default | Extra   | 
+----------------+---------------------+------+-----+---------+----------------+ 
| id    | int(10) unsigned | NO | PRI | NULL | auto_increment | 
| participant_id | int(10) unsigned | NO | MUL | NULL |    | 
| message  | text    | NO |  | NULL |    | 
| time_cre  | int(11)    | NO |  | NULL |    | 
+----------------+---------------------+------+-----+---------+----------------+ 

我想与convo_id的每个沿回的最新消息,(通过msg_messages.time_cre)我的逻辑如下:1)获取用户参与的所有对话ID(由msg_participants.prof_type和msg_participants.prof_id定义)。 2)根据每个对话ID获取每个参与者。 3)返回每个对话中的最后一条消息(按msg_messages.time_cre排序)。该功能应与Facebook返回对话列表以及每次对话中最后一条消息的预览类似。

这里是我到目前为止的代码:

SELECT msg_participants.convo_id, 
    msg_messages.id, 
    msg_participants.prof_id, 
    msg_participants.prof_type, 
    SUBSTRING(msg_messages.message,1,50) as message, 
    msg_messages.time_cre 
FROM msg_participants,msg_messages 
WHERE 
    msg_participants.id IN 
     (SELECT participant_id FROM 
      (SELECT * FROM (SELECT msg_messages.participant_id,msg_participants.convo_id FROM msg_messages,msg_participants WHERE msg_messages.participant_id=msg_participants.id AND participant_id IN 
       (SELECT id FROM msg_participants WHERE convo_id IN (SELECT convo_id FROM msg_participants WHERE prof_id=PROFILE_ID_HERE AND prof_type=PROFILE_TYPE_HERE)) 
      ORDER BY time_cre DESC) AS tbl1 
      GROUP BY tbl1.convo_id) AS tbl2) 

    AND msg_messages.id IN 
     (SELECT id FROM (SELECT * FROM (SELECT msg_messages.id,msg_participants.convo_id FROM msg_messages,msg_participants WHERE msg_messages.participant_id=msg_participants.id AND participant_id IN 
       (SELECT id FROM msg_participants WHERE convo_id IN (SELECT convo_id FROM msg_participants WHERE prof_id=PROFILE_ID_HERE AND prof_type=PROFILE_TYPE_HERE)) 
      ORDER BY time_cre DESC) AS tbl3 
      GROUP BY tbl3.convo_id) AS tbl4) 

我知道怎么难看/哈克我的代码,但我在那里我已经重写很多次了,我只是想要的东西的工作点。此代码是...关闭。但它返回msg_participants.id X msg_messages.id的笛卡尔积。对于我为什么它返回笛卡尔产品是有道理的,但我不知道如何使这个查询做我想做的事情。

谢谢!

+0

我觉得你不理解数据库如何关系的工作。需要有关系。这意味着加入SQL。但是你的模型中似乎没有关于连接的数据。你怎么知道一个消息来自哪一个对话。你只知道是谁说的......但人们可以有多个对话。无论如何,你的查询应该有JOIN语句而不是IN语句。 – Hogan 2014-10-16 19:43:16

+0

我正在使用连接,只是不明确。他们通过msg_messages.participant_id - > msg_participants.id - > msg_participants.convo_id知道消息来自哪个对话。 – William 2014-10-16 19:54:54

回答

0

您可以使用not existsmsg_messages选择最新行对给定participant_id

select mp.convo_id, mm.id, mp.prof_id, mp.prof_type, mm.message, mm.time_cre 
from msg_participants mp 
join msg_messages mm on mp.id = mm.participant_id 
where mp.prof_id = PROF_ID_HERE 
and mp.prof_type = PROF_TYPE_HERE 
and not exists (
    select 1 from msg_messages mm2 
    where mm2.participant_id = mm.participant_id 
    and mm2.time_cre > mm.time_cre 
)