MySQL选择在状态数组中的位置,最后出现等于特定的值

发布于 2025-01-17 14:11:34 字数 1750 浏览 6 评论 0原文

假设我有一个这样的表:

id  user_id status  updateded_id
1   1       yes     2/5/2013
2   2       no      2/1/2013
3   3       yes     1/28/2013
4   2       yes     1/24/2013
5   2       no      1/20/2013
6   1       yes     1/16/2013
7   3       no      1/12/2013
8   4       yes     1/8/2013
9   5       yes     1/4/2013
10  5       no      12/31/2012
11  6       yes     12/27/2012
12  7       no      12/23/2012
13  6       yes     12/19/2012
14  4       yes     12/15/2012
15  3       no      12/11/2012
16  3       yes     12/7/2012
17  1       no      12/3/2012
18  1       yes     11/29/2012

我需要编写一个查询,仅当最新 updated_id 的状态为“yes”时才选择 user_id

例如,将选择 user_id 1,而不会选择 user_id 2。 我在复杂的 Larvel 查询生成器(Larvel 版本 4.2)中编写了这个。

结果是多个用户。 因此,在此查询中,仅当状态表中最后一次出现的情况为“是”时,才应选择用户一次。

我尝试过:

    $query->join('mytable', function ($join) use ($statuses) {
            ->whereRaw('mytable.status in ('.$statuses.') )
            ->orderBy('mytable.updated_at', 'desc')
            ->limit(1);
    });

AND:

    $query->join('statuses', function ($join) use ($statuses) {
            ->whereIn('mytable.status ,$statuses )
            ->orderBy('mytable.updated_at', 'desc')
            ->limit(1);
    });

在 whereIn 和 whereRaw 函数上出现错误。就像他们不存在一样。

所以,进步。通过此查询,我可以找到一个用户的最新状态为“是”。我找不到一种方法让它通过这种逻辑选择所有用户:

SELECT  s1.user_id 
FROM statuses s1
WHERE s1.user_id  = ( SELECT user_id  
                   FROM statuses s2
                   WHERE s2.status IN ('yes', 'no') ORDER BY s2.updateded_id DESC LIMIT 1) 
                   AND s1.status= 'yes';

Let's say I have a table like this:

id  user_id status  updateded_id
1   1       yes     2/5/2013
2   2       no      2/1/2013
3   3       yes     1/28/2013
4   2       yes     1/24/2013
5   2       no      1/20/2013
6   1       yes     1/16/2013
7   3       no      1/12/2013
8   4       yes     1/8/2013
9   5       yes     1/4/2013
10  5       no      12/31/2012
11  6       yes     12/27/2012
12  7       no      12/23/2012
13  6       yes     12/19/2012
14  4       yes     12/15/2012
15  3       no      12/11/2012
16  3       yes     12/7/2012
17  1       no      12/3/2012
18  1       yes     11/29/2012

I need to write a query that selects a user_id only if the status at the latest updateded_id is 'yes`.

For example user_id 1 will be choosen and user_id 2 won't.
I write this in a complex Larvel query builder (Larvel version 4.2).

The result is multiple users.
So in this query user is should be selected only once and only if the last occurrence in statuses table be yes.

I tried:

    $query->join('mytable', function ($join) use ($statuses) {
            ->whereRaw('mytable.status in ('.$statuses.') )
            ->orderBy('mytable.updated_at', 'desc')
            ->limit(1);
    });

AND:

    $query->join('statuses', function ($join) use ($statuses) {
            ->whereIn('mytable.status ,$statuses )
            ->orderBy('mytable.updated_at', 'desc')
            ->limit(1);
    });

Got error on whereIn and whereRaw function. Like they don'y exist.

So, Progress. Whit this query I can find one user that has the has 'yes' as the newest status. I can not find a way to make it choose all the user by this logic:

SELECT  s1.user_id 
FROM statuses s1
WHERE s1.user_id  = ( SELECT user_id  
                   FROM statuses s2
                   WHERE s2.status IN ('yes', 'no') ORDER BY s2.updateded_id DESC LIMIT 1) 
                   AND s1.status= 'yes';

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浪漫之都 2025-01-24 14:11:34

你可以这样做。

一个用户有多个状态,您想根据最新的状态获取用户,对吗?

Eloquent 用户模型

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    /*
     * Get the user's statuses
     */
    public function statuses()
    {
        return $this->hasMany(Status::class);
    }

    /**
     * Get the user's latest status.
     */
    public function latestStatus()
    {
        return $this->hasOne(Status::class)->latestOfMany('updated_at');
    }
}

然后你可以像这样编写查询

return User::whereHas('latestStatus', function($query) {
    $query->where('status', 'yes');
})->get();

You could do this.

A user has multiple statuses and you want to get the users based on the latest status right?

Eloquent user model

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    /*
     * Get the user's statuses
     */
    public function statuses()
    {
        return $this->hasMany(Status::class);
    }

    /**
     * Get the user's latest status.
     */
    public function latestStatus()
    {
        return $this->hasOne(Status::class)->latestOfMany('updated_at');
    }
}

Then you can write the query like this

return User::whereHas('latestStatus', function($query) {
    $query->where('status', 'yes');
})->get();
谁的年少不轻狂 2025-01-24 14:11:34

我相信您正在寻找我所说的“过滤器加入”。我对Laravel不熟悉,所以请接受MySQL:

SELECT t.user_id FROM table AS t
LEFT JOIN table AS filter ON (
  filter.user_id = t.user_id
  AND filter.updated_id > l.updated_id
)
WHERE filter.id IS NULL
AND t.status = 'yes';

使用您在问题中提供的数据,我的输出是:

1
3
4
5
6

我相信满足“具有'是'作为其最新状态的用户的要求”。

I believe you're looking for what I call a "filter join." I'm not familiar with Laravel, so please accept MySQL:

SELECT t.user_id FROM table AS t
LEFT JOIN table AS filter ON (
  filter.user_id = t.user_id
  AND filter.updated_id > l.updated_id
)
WHERE filter.id IS NULL
AND t.status = 'yes';

Using the data you provided in the question, my output was:

1
3
4
5
6

I believe that satisfies the requirements of "users that have 'yes' as their newest status."

吲‖鸣 2025-01-24 14:11:34

您可以在 MySql 查询中使用 NOT EXISTS ,(我希望)您可以将其转换为 Laravel 代码:

SELECT s1.user_id 
FROM statuses s1
WHERE s1.status = 'yes'
  AND NOT EXISTS (
            SELECT 1
            FROM statuses s2
            WHERE s2.user_id = s1.user_id AND s2.updateded_id > s1.updateded_id
          );

请参阅 演示

You can use NOT EXISTS in a MySql query, which (I hope) you can transform to Laravel code:

SELECT s1.user_id 
FROM statuses s1
WHERE s1.status = 'yes'
  AND NOT EXISTS (
            SELECT 1
            FROM statuses s2
            WHERE s2.user_id = s1.user_id AND s2.updateded_id > s1.updateded_id
          );

See the demo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文