我已经用所需的过滤器查询了user_invitations.inviter_id:
select
inviter_id,
count(inviter_id) as invited_cnt
from user_invitations
group by
inviter_id
having
invited_cnt >= 52
order by
invited_cnt
在这里,我尝试仅使用邀请者ID来获取另一个名为用户的表中的ID(即,要获取与我刚刚获得的邀请者ID相匹配的user_id)
我尝试做内在的调动
from users
inner join (
select inviter_id, count(inviter_id) as invited_cnt
from user_invitations
group by inviter_id
having invited_cnt >= 52
order by invited_cnt
)user_inv
on users.id = user_inv.id;
不走运,我考虑过使用子查询
from users
where id in (
select inviter_id
from user_invitations
where inviter_id in (
select count(inviter_id) as invited_cnt
from user_invitations
group by inviter_id
having invited_cnt >= 52
)
group by inviter_id
);
但是在大多数内部查询中,我无法找到一种方法来通过vited_cnt> = 52进行过滤后返回inviter_id。我如何实现这一点,因为使用了inved_cnt> = 52,我们是否需要总计数是不可能的吗? / p>
我查看了一些资源: Select id from one query and use it in another
但它们似乎都不处理聚合。
答案 0 :(得分:1)
如果我理解您的问题,则此查询应为您提供%{
#include <iostream>
using namespace std;
extern int yylex();
%}
%option noyywrap
%%
. { fwrite(yytext, yyleng, 1, yyout); }
%%
int main(int argc, char *argv[])
{
FILE *sourceFile = fopen("swflow.dfd", "r");
FILE *genFile;
if(sourceFile) {
genFile = fopen("swflow.v", "w+");
}
else {
cout << "Cannot open source file!" << endl;
return -1;
}
yyin = sourceFile;
yyout = genFile;
while(yylex());
return 0;
}
中与user_id
匹配的user
inviter_id
如果您只期望一个SELECT
us.inviter_id,
count(us.inviter_id) AS invited_cnt,
u.user_id
FROM user_invitations AS us
JOIN user AS u ON u.user_id = us.inviter_id //just change the column name if needed
GROUP BY
us.inviter_id
HAVING
invited_cnt >= 52
ORDER BY
invited_cnt
,那么我认为这应该可行。尽管包括表结构以及一些示例数据和预期输出会很好