0

我非常感谢任何尝试简化 MySQL 查询的帮助。查询的目的是从消息表 (users_messages) 中检索消息,该表具有以下列:message_id、from_id、to_id、message_content、date_sent。

from_id 和 to_id 需要加入一个包含以下列的用户表(用户):user_id,user_username。

另外我应该提到有一个被阻止的用户表(users_blocked),如果该表中的 user_id 功能,它会过滤掉任何消息。

所有这些都可以正常工作,并且消息以最新的优先顺序排序,这正是我想要的。我唯一的问题是它没有提取相应的“message_content”。即它正在拉最近的日期,但不是最近的消息。

也许我需要一种不同的方法(例如子查询),但我无法理解它。

这是查询:

select m.message_content,
    if(from_id < to_id, concat(from_id,to_id), concat(to_id,from_id)) as ft,
    if (from_id = $my_id, to_id, from_id) as other_id,
    max(date_sent) as most_recent
from users_messages m
    left join users_blocked ub1 on (from_id = ub1.blocked_id and ub1.user_id = $my_id)
    left join users_blocked ub2 on (to_id = ub2.blocked_id and ub2.user_id = $my_id)
where
    (from_id = $my_id or to_id = $my_id)
    and ub1.blocked_id is null
    and ub2.blocked_id is null
group by
    ft
order by
    most_recent desc

对不起,这里是表结构:

用户

用户 ID 用户用户名
1西蒙
2 琥珀色
3 汤姆

users_messages

message_id from_id to_id date_sent message_content
1 1 2 2012-07-04 11:52:12 你好
2 1 2 2012-07-04 12:32:24 另一个消息
3 1 2 2012-07-04 14:00:00 再次你好

users_blocked

user_id 被阻止的_id
1 3
4

3 回答 3

0

据我了解,此请求的主要问题是结果仅包含第一个日期,而不包含消息。要解决此问题,您可以执行以下操作:

  1. 制作具有最新日期的准备好的数据集:

    选择 to_id, from_id, max(date_sent) as most_recent from users_messages m left join users_blocked ub on ub.user_id = $my_id and ub.blocked_id in (to_id, from_id) where (from_id = $my_id or to_id = $my_id) and ub. blocked_id 按 to_id 为空组,按 most_recent desc 排序 from_id

我看到您通过 to_id、from_id 两个列来摸索数据。此子查询不是计算以下内容的最佳位置:

if(from_id < to_id, concat(from_id,to_id), concat(to_id,from_id)) as ft
  1. 然后从 users_messages 中选择其他需要的数据,这些数据与我们准备好的表中的 to_id、from_id 和 recent_date 匹配:

    选择 um.* from

    (
    select to_id, from_id, max(date_sent) as most_recent
    from users_messages m
    left join users_blocked ub on ub.user_id = 1
    and ub.blocked_id in (to_id, from_id)
    where
    (from_id = 1 or to_id = 1)
    and ub.blocked_id is null
    group by
    to_id, from_id
    order by
    most_recent desc
    ) as prepared_messages
    left join users_messages um on um.from_id = prepared_messages.from_id
    and um.to_id = prepared_messages.to_id
    and um.date_sent = prepared_messages.most_recent
    
于 2013-07-05T12:13:45.833 回答
0

在这里,我假设 SimonKing 想要来自 users_messages 表的消息内容,其中包括以下条件,

  1. 用户不应阻止任一方向,
  2. 用户之间传输的最新消息

因此,我将 Mark Ba​​nnister 查询修改如下,

SELECT temp.* FROM  (
SELECT um.*, concat(um.from_id,to_id) as direction FROM userMessages um
LEFT JOIN userBlocked ub1 ON um.from_id = ub1.user_id AND um.to_id = ub1.blocked_id
LEFT JOIN userBlocked ub2 ON um.to_id = ub2.user_id AND um.from_id = ub2.blocked_id 
WHERE ub1.user_id is null AND ub1.blocked_id is null AND ub2.user_id is null AND ub2.blocked_id is null
ORDER BY um.date_sent DESC
) temp
GROUP BY direction

SQL小提琴是http://sqlfiddle.com/#!2/bdc77/1/0

于 2013-07-04T14:48:14.937 回答
0

尝试:

select m.message_content,
       x.ft,
       x.other_id,
       x.most_recent
from (select if(from_id < to_id, concat(from_id,to_id), concat(to_id,from_id)) as ft,
             if(from_id = $my_id, to_id, from_id) as other_id,
             max(date_sent) as most_recent
      from users_messages um
      left join users_blocked ub1 
        on (um.from_id = ub1.blocked_id and ub1.user_id = $my_id)
      left join users_blocked ub2 
        on (um.to_id = ub2.blocked_id and ub2.user_id = $my_id)
      where ub1.blocked_id is null and ub2.blocked_id is null and
            (um.from_id = $my_id or um.to_id = $my_id)
      group by ft) x
join users_messages m
     on m.date_sent = x.most_recent and 
        m.from_id in ($my_id, x.other_id) and 
        m.to_id in ($my_id, x.other_id)
order by
    x.most_recent desc

SQLFiddle在这里

于 2013-07-04T13:01:10.010 回答