Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 试图简化MySQL查询_Php_Mysql_Sql - Fatal编程技术网

Php 试图简化MySQL查询

Php 试图简化MySQL查询,php,mysql,sql,Php,Mysql,Sql,我非常感谢任何帮助我简化MySQL查询的人。查询的目的是从messages表users\u messages中检索消息,该表具有以下列:message\u id、from\u id、to\u id、message\u content、date\u sent from_id和to_id需要加入一个users表users,该表包含以下列:user_id、user_username 此外,我还应该提到,有一个阻塞的users表users\u blocked,它会过滤掉该表中的user\u id功能所显

我非常感谢任何帮助我简化MySQL查询的人。查询的目的是从messages表users\u messages中检索消息,该表具有以下列:message\u id、from\u id、to\u id、message\u content、date\u sent

from_id和to_id需要加入一个users表users,该表包含以下列:user_id、user_username

此外,我还应该提到,有一个阻塞的users表users\u blocked,它会过滤掉该表中的user\u id功能所显示的任何消息

所有这些都可以很好地工作,并且邮件都是以最新的优先顺序订购的,这正是我想要的。我唯一的问题是它没有提取相应的“消息内容”。i、 它是最近的日期,但不是最近的消息

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

以下是查询:

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
对不起,以下是表格结构:

使用者

用户信息

message_id from_id to_id date_sent message_content 1 1 2 2012-07-04 11:52:12 Hello 2 1 2 2012-07-04 12:32:24 Another message 3 1 2 2012-07-04 14:00:00 Hello again 用户被阻止

user_id blocked_id 1 3 尝试:


SQLFiddle。

这里我假设SimonKing想要,来自users\u messages表的消息内容,包括以下条件

用户不应阻挡任何方向, 在用户之间传输的最新消息 因此,我修改了Mark Bannister查询,如下所示

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 fiddle是

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

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

选择to_id、from_id、maxdate_作为最近发送 来自用户的消息 左加入用户\u阻止ub上的ub。用户\u id=$my\u id 和ub.blocked_id in to_id,from_id 哪里 from_id=$my_id或to_id=$my_id 并且ub.u id为空 分组 到\u id,从\u id 订购人 最近的描述

我看到您通过两个collumn从\u id到\u id摸索数据。此子查询不是计算以下内容的最佳位置:

if(from_id < to_id, concat(from_id,to_id), concat(to_id,from_id)) as ft

请包括您的表格结构。另外-它返回消息内容,但不是正确的值?或者它什么也不退?还不错。见鬼,你不用滚动就可以全部阅读,这是一个额外的奖励!这并不可怕,但我可能只会得到一系列被阻止的用户,并为from_IDI做一个“不在”,谢谢你的评论。问题是它返回的是最近的日期,但却是第一条消息的内容。i、 e.日期:2012-07-04 14:00:00,消息:你好小注:你计划在你的用户中有大量的数据集吗?消息我知道“巨大”是一个相对的词?但如果是这样的话,你应该继续关注你的团队。这可能会成为以后的瓶颈,因为mysql无法使用任何索引来优化组,因此可能会变慢。谢谢Mark。我尝试了这个方法,在“where子句”中收到了一个未知列“m.from_id”的错误。我在子查询中将users\u messages更改为users\u messages m,现在返回0个结果。@SimonKing:我做了一些更改-现在试试。点击。绝对完美。多谢各位much@SimonKing:很高兴我能帮上忙。这依赖于MySQL中观察到的但未记录的行为,这在未来版本中可能不起作用;它已经从MySQL的开源分支中删除了。@马克·班尼斯特,给我一些url来获取更多信息
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
if(from_id < to_id, concat(from_id,to_id), concat(to_id,from_id)) as ft
(
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