为什么我没有从 Rails 的 ifnull 中获取值
我有一个查询(很长,但要点如下)
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有收到响应,因为您对 groups.name 的过滤器位于
WHERE
子句中。这将仅返回棒球组中的用户。如果您在JOIN
处添加条件,那么您将返回不属于棒球组的用户,并且他们将具有预期的 NULL 值: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 theJOIN
, then you'll get users back that are not part of the baseball group, and they'll have the expected NULL value: