0

所以我想在一列中搜索最大时间:

sqlite> SELECT max(strftime('%Y-%m-%d %H:%M',time)) FROM posts WHERE thread_id=123456;
2012-10-02 02:31

对于此查询返回的每个 thread_id:

sqlite> SELECT thread_id FROM threads WHERE out_of_date=0;
111
123
187
...

然后我想搜索其字段与第一个查询返回的last_post字段不匹配的所有线程time,并将该out_of_date字段设置为1:

sqlite> UPDATE threads SET out_of_date=1 WHERE thread_id=123456 AND last_post!='2012-10-02 02:31';

问题是,我不太确定应该如何组合这三个单独的查询。有什么想法吗?

4

1 回答 1

1

The below SQL should update the threads table correctly. It uses a correlated sub-query to combine the 1st and 3rd of your queries. This can then be combined with your 2nd query by adding it's WHERE clause.

UPDATE threads T
SET out_of_date = 1
WHERE out_of_date = 0
    AND last_post != (
        SELECT MAX(strftime('%Y-%m-%d %H:%M',time))
        FROM posts
        WHERE thread_id = T.thread_id
    )
;
于 2012-10-04T21:55:30.007 回答