我正在研究vBulletin論壇。這個想法是創建一個網頁,管理員可以查詢所有最新帖子並「讀取」它們,以便將它們標記爲「已檢查」。Mysql計算結果在where子句中?
該網頁的工作原理非常好,但是,查詢速度有點慢,因爲我們每天都會收到新帖子的貼子,現在它正在拉取過去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.lastpost-threadread.readtime > 0
我的第一個解決方案是隻需添加
AND thread.lastpost-threadread.readtime > 0
到where子句。然而,這引起了
LEFT JOIN threadread AS threadread ON (threadread.threadid = thread.threadid AND threadread.userid = 90122)
,只選擇threadread.userid = 90122
,而不是threadread.threadid = thread.threadid
所以我的想法是做
SELECT ... thread.lastpost-threadread.readtime AS isread
,然後AND isread > 0
。這返回的錯誤
未知列「isread」
我最有可能試圖做一些事情非常愚蠢的或根本不明白這是如何entiry查詢工作,但我出來關於如何解決我的問題的想法。所以現在我問你們:)
一些示例數據會很好。我無法遵循你的問題。 –
@TimBiegeleisen這是小提琴:http://sqlfiddle.com/#!9/7dd95 – icecub