为什么我没有从 Rails 的 ifnull 中获取值

发布于 2024-11-29 04:49:54 字数 526 浏览 4 评论 0原文

我有一个查询(很长,但要点如下)

Name.find_by_sql(['SELECT IFNULL(users.last_name, "nil") FROM users
                  JOIN user_groups ON user_groups.user_id = users.id
                  JOIN groups ON groups.id = user_groups.grous_id
                  WHERE users.first_name IN (?)', ['Sam','Sally']]) AND groups.name='baseball'

现在 Sam 在棒球队中,但 Sally 不在。

我希望我能回来(山姆的姓氏是希尔),

['Hill','nil']

但我只得到[“希尔”]。 我不知道为什么我没有得到“零”返回。 我也在每个连接语句上尝试过“LEFT OUTER JOIN”,因为我认为这可能是“nil”丢失的地方,但我仍然只返回了一项。

I've got a query (it's quite long, but here's the gist of it)

Name.find_by_sql(['SELECT IFNULL(users.last_name, "nil") FROM users
                  JOIN user_groups ON user_groups.user_id = users.id
                  JOIN groups ON groups.id = user_groups.grous_id
                  WHERE users.first_name IN (?)', ['Sam','Sally']]) AND groups.name='baseball'

Now Sam is in the baseball group, but Sally is not.

I would hope that I would get back (Sam's last name is Hill)

['Hill','nil']

But I'm only getting ['Hill'].
I'm not sure why I don't get the 'nil' returned.
I've tried 'LEFT OUTER JOIN' on each of the join statements as well, as I thought maybe that is where the 'nil' was getting lost, but I'm still only getting the one item returned.

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

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

发布评论

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

评论(1

2024-12-06 04:49:54

您没有收到响应,因为您对 groups.name 的过滤器位于 WHERE 子句中。这将返回棒球组中的用户。如果您在 JOIN 处添加条件,那么您将返回不属于棒球组的用户,并且他们将具有预期的 NULL 值:

JOIN groups ON groups.id = user_groups.grous_id AND groups.name='baseball'

You're not getting a response back because your filter on groups.name is in the WHERE clause. This will only return users that are in the baseball group. If you add the condition at the JOIN, then you'll get users back that are not part of the baseball group, and they'll have the expected NULL value:

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