【发布时间】:2021-05-30 02:49:19
【问题描述】:
我编写了一个查询,用于检查在对话中的用户删除后写入对话的消息。这是我的查询:
$messages = DB::table('conversations')
->where('conversations.id', $conversation->id)
->join('messages', 'messages.conversation_id', '=', 'conversations.id')
->join('deleted_conversations', 'deleted_conversations.conversation_id', '=', 'conversations.id')
->where('messages.created_at', '>=', 'deleted_conversations.created_at')
->orderBy('messages.created_at', 'desc')
->select('messages.*')
->get()
->all();
但每次我尝试使用where('messages.created_at, '<', 'deleted_conversations_created_at 语句运行此查询时,如果没有精确检查日期。为什么?
这里是迁移:
Schema::create('conversations', function (Blueprint $table) {
$table->id();
$table->integer('sender_id');
$table->integer('receiver_id');
$table->enum('status', ['read', 'unread']);
$table->timestamps();
});
Schema::create('messages', function (Blueprint $table) {
$table->id();
$table->integer('conversation_id');
$table->integer('sender_id');
$table->string('message');
$table->timestamps();
});
Schema::create('deleted_conversations', function (Blueprint $table) {
$table->id();
$table->integer('conversation_id');
$table->integer('user_id');
$table->timestamps();
});
deleted_conversations 表
| id | conversation_id | user_id | created_at | updated_at |
|---|---|---|---|---|
| 1 | 1 | 2 | 2021-02-28 11:26:36 | 2021-02-28 11:30:45 |
对话表
| id | sender_id | user_id | receiver_id | status | created_at | updated_at |
|---|---|---|---|---|---|---|
| 1 | 1 | 1 | 2 | unread | 2021-02-28 11:07:09 | 2021-02-28 11:31:50 |
消息表
| id | conversation_id | sender_id | message | created_at | updated_at |
|---|---|---|---|---|---|
| 1 | 1 | 1 | S.A | 2021-02-28 00:00:00 | 2021-02-28 00:00:00 |
| 2 | 1 | 2 | A.S | 2021-02-28 00:00:00 | 2021-02-28 00:00:00 |
【问题讨论】:
-
您能否提供说明问题的示例数据?您是日期列文本还是实际日期?
-
我将编辑问题。日期列是使用
$table->timestamps()创建的。
标签: php laravel query-builder