Mysql计算结果在where子句中?

Mysql计算结果在where子句中?,mysql,Mysql,我在一个vBulletin论坛上工作。其想法是创建一个网页,管理员可以在其中查询所有最新的帖子并“阅读”它们,从而将它们标记为“已检查” 这个网页运行得很好,但是,查询速度有点慢,因为我们每天都会收到成千上万的新帖子,而现在它正在从过去的24小时中提取所有帖子 为了解决这个问题,我希望它只拉还没有检查的帖子 原始查询如下: SELECT thread.forumid, thread.firstpostid, thread.lastpost, thread.lastposter, thread.

我在一个vBulletin论坛上工作。其想法是创建一个网页,管理员可以在其中查询所有最新的帖子并“阅读”它们,从而将它们标记为“已检查”

这个网页运行得很好,但是,查询速度有点慢,因为我们每天都会收到成千上万的新帖子,而现在它正在从过去的24小时中提取所有帖子

为了解决这个问题,我希望它只拉还没有检查的帖子

原始查询如下:

SELECT 
thread.forumid, thread.firstpostid, thread.lastpost, thread.lastposter, thread.lastpostid, thread.replycount, 
thread.threadid, thread.title, thread.open, thread.views, post.pagetext AS preview, post.userid AS lastpuserid, threadread.readtime AS readtime, threadread.userid AS userid
FROM thread AS thread
LEFT JOIN deletionlog AS deletionlog ON (thread.threadid = deletionlog.primaryid AND deletionlog.type = 'thread')
LEFT JOIN post AS post ON (post.postid = thread.lastpostid)
LEFT JOIN threadread AS threadread ON (threadread.threadid = thread.threadid AND threadread.userid = 90122)
WHERE open <> 10
AND thread.lastpost >=  1494100000
AND thread.forumid NOT IN(0013482730313233343537)
AND thread.visible = '1'
AND post.visible = 1
AND deletionlog.primaryid IS NULL

GROUP BY thread.threadid
ORDER BY thread.lastpost DESC
选择
thread.forumid、thread.firstpostid、thread.lastpost、thread.lastposter、thread.lastpostid、thread.replycount、,
thread.threadid、thread.title、thread.open、thread.views、post.pagetext作为预览、post.userid作为lastpuserid、threadread.readtime作为readtime、threadread.userid作为userid
以线为线
左键将deletionlog作为deletionlog打开(thread.threadid=deletionlog.primaryid,deletionlog.type='thread')
将post作为post ON(post.postid=thread.lastpostid)左连接post
左连接threadread作为threadread打开(threadread.threadid=thread.threadid和threadread.userid=90122)
哪里开10号
和thread.lastpost>=149410000
和thread.forumid不在(00134827303132333435337)
和thread.visible='1'
和post.visible=1
并且deletionlog.primaryid为空
按线程分组。线程ID
按线程排序。lastpost DESC
为了只得到未检查的帖子,我需要计算

thread.lastpost-thread.readtime>0

我的第一个解决方案是添加

和thread.lastpost-thread.readtime>0
到where子句。然而,这导致了

LEFT JOIN threadread AS threadread ON(threadread.threadid=thread.threadid和threadread.userid=90122)

只选择
threadread.userid=90122
而不选择
threadread.threadid=thread.threadid

所以我的想法是

选择。。。thread.lastpost-threadread.readtime作为isread

然后是
和isread>0
。这返回了错误

未知列“isread”


我很可能试图做一些非常愚蠢的事情,或者根本不理解这个实体查询是如何工作的,但是我对如何解决我的问题没有想法。所以现在我要问大家:)

如果要使用表别名,请缩短名称,以便更易于编写和读取查询

然后,您需要修复
JOIN
s。
WHERE
子句将一些外部联接转换为内部联接。然后,将条件移动到相应的
ON
子句

我想这就是你想要的:

SELECT t.forumid, t.firstpostid, t.lastpost, t.lastposter, t.lastpostid, t.replycount, 
       t.threadid, t.title, t.open, t.views, p.pagetext AS preview, p.userid AS lastpuserid, tr.readtime AS readtime, tr.userid AS userid
FROM thread t JOIN
     post p
     ON p.postid = t.lastpostid LEFT JOIN
     deletionlog dl
     ON t.threadid = dl.primaryid AND dl.type = 'thread' LEFT JOIN
     threadread tr
     ON tr.threadid = t.threadid AND tr.userid = 90122 AND
        t.lastpost > tr.readtime
WHERE open <> 10 AND
      t.lastpost >=  1494100000 AND
      t.forumid NOT IN (0013482730313233343537) AND
      t.visible = 1 AND
      p.visible = 1 AND
      dl.primaryid IS NULL
GROUP BY t.threadid
ORDER BY t.lastpost DESC;
选择t.forumid、t.firstpostid、t.lastpost、t.lastposter、t.lastpostid、t.replycount、,
t、 threadid、t.title、t.open、t.views、p.pagetext作为预览、p.userid作为lastpuserid、tr.readtime作为readtime、tr.userid作为userid
从线程t连接
邮政p
在p.postid=t.lastpostid左连接上
删除日志
在t.threadid=dl.primaryid和dl.type='thread'左连接上
threadread tr
在tr.threadid=t.threadid和tr.userid=90122和
t、 lastpost>tr.readtime
在哪里开10和10
t、 lastpost>=149410000和
t、 forumid不在(00134827303132333435337)和
t、 可见=1和
p、 可见=1和
dl.primaryid为空
按t.threadid分组
由t.lastpost DESC订购;

我不确定
群组BY
应该做什么。在未聚合列上使用没有聚合函数的
groupby
通常是一个非常糟糕的主意。在本例中,
t.threadid
可能是该表的主键,因此该表中的其他列也可以。但是,如果连接到其他表时生成了重复项,该怎么办?

一些示例数据会很好。我听不懂你的问题。@TimBiegeleisen问题是:匿名否决票的原因是什么?我很确定你的问题是,
WHERE
条件将外部连接变成了内部连接,这就是为什么我建议把这个条件放在子句中。你介意用一个例子来编辑你的答案吗?我自己并没有写这个查询,虽然我了解其中的大部分内容,但我的知识在连接方面还不够。我了解它们是如何工作的,但我不知道如何在condition@icecub . . . 答案是肯定的。我将条件更改为
t.lastpost>tr.readtime
,因为这看起来更清楚。啊,好吧。我明天会集中讨论这个问题+感谢你给了我很多宝贵的建议,并试图帮助我摆脱这种不公平的否决票。谢谢:)