按删除的时间戳排序并从特定ID开始返回记录

时间:2022-06-18 22:44:39

I have a messages table, and I want to get the deleted messages. They are determined by not null timestamp, and I want them starting from user-specified id. First I thought that this would be just a simple query (I use 48 as example):

我有一个消息表,我想获取已删除的消息。它们由非空时间戳确定,我希望它们从用户指定的id开始。首先我认为这只是一个简单的查询(我以48为例):

select id from messages where deleted_at <> "NULL" and id > 48 order by deleted_at;

Shortly I noticed that this does not return the expected result. Here are some results from my dataset:

不久我注意到这并没有返回预期的结果。以下是我的数据集的一些结果:

    # without id > 48
    46,54,53,52,51,50,49,48,61,63,62

    # with id > 48
    49,50,51,52,53,54,61,62,63

    # so I just want to cut the result from a specified value, like so
    46,54,53,52,51,50,49,48 V 61,63,62
                            % *snip*

    # expected result
    61,63,62

(I added the scissors to illustrate the problem)

(我加了剪刀来说明问题)

How can I build a query to do so? I am also open for a Laravel-only solution, but just getting a SQL way would be good. I know the limiting could be done with Laravel-side PHP scripting, but it has a great cost when this data is pinged once per second.

如何构建查询来执行此操作?我也开放了一个仅限Laravel的解决方案,但只是采用SQL方式会很好。我知道可以使用Laravel端的PHP脚本来完成限制,但是当这个数据每秒被ping一次时,它的成本很高。

2 个解决方案

#1


1  

NULL is not a String

NULL不是String

select id from messages where deleted_at is not null and id > 48 order by deleted_at;

#2


0  

With Laravel, you can use WhereNotNull, e.g.:

使用Laravel,你可以使用WhereNotNull,例如:

$messages = DB::table('messages')
                    ->whereNotNull('deleted_at')
                    ->orderBy('deleted_at', 'desc')
                    ->get();

#1


1  

NULL is not a String

NULL不是String

select id from messages where deleted_at is not null and id > 48 order by deleted_at;

#2


0  

With Laravel, you can use WhereNotNull, e.g.:

使用Laravel,你可以使用WhereNotNull,例如:

$messages = DB::table('messages')
                    ->whereNotNull('deleted_at')
                    ->orderBy('deleted_at', 'desc')
                    ->get();