如何重写 JOIN 中的子查询?
我对 SQL 不太熟悉,我的问题是如何重写以下语句以使其看起来更自然。我试图编写的 select
连接两个表——“users”和“stats”——并且我提前知道用户的 id。这可能是非常基本的东西,但我还不是 SQL 忍者。
select
u.id,
sum(s.xxx)
from
(
select id from users where id in (100, 200, 300)
) u
left join
stats s
on u.id = s.user_id
group by
u.id
;
看起来奇怪的部分是
(
select id from users where id in (100, 200, 300)
) u
建议我正确的方式。谢谢
I'm not very fluent in SQL and my question is how can I rewrite the following statement to make it look more natural. The select
I'm trying to write joins two tables -- "users" and "stats" -- and I know id's of users in advance. It's probably something very basic but I'm not an SQL ninja yet.
select
u.id,
sum(s.xxx)
from
(
select id from users where id in (100, 200, 300)
) u
left join
stats s
on u.id = s.user_id
group by
u.id
;
The part that looks strange is
(
select id from users where id in (100, 200, 300)
) u
Suggest me the right way. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一种复杂的说法
....
WHERE id in (100,200,300)
在您的
WHERE
子句中。整个事情可以重写为:
That's a complicated way of saying
....
WHERE id in (100,200,300)
In your
WHERE
clause.The whole thing could be rewritten as: