MySQL选择在状态数组中的位置,最后出现等于特定的值
假设我有一个这样的表:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以这样做。
一个用户有多个状态,您想根据最新的状态获取用户,对吗?
Eloquent 用户模型
然后你可以像这样编写查询
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
Then you can write the query like this
我相信您正在寻找我所说的“过滤器加入”。我对Laravel不熟悉,所以请接受MySQL:
使用您在问题中提供的数据,我的输出是:
我相信满足“具有'是'作为其最新状态的用户的要求”。
I believe you're looking for what I call a "filter join." I'm not familiar with Laravel, so please accept MySQL:
Using the data you provided in the question, my output was:
I believe that satisfies the requirements of "users that have 'yes' as their newest status."
您可以在 MySql 查询中使用
NOT EXISTS
,(我希望)您可以将其转换为 Laravel 代码:请参阅 演示。
You can use
NOT EXISTS
in a MySql query, which (I hope) you can transform to Laravel code:See the demo.