在 Ruby Sequel 库的 "having" 块中使用相等比较
Using equality comparison in Ruby Sequel library's "having" block
我正在尝试通过 Sequel 在 Ruby(Ruby 2.5.7,Sequel 5.28. 0), 但在使用相等比较时遇到问题。
所有文档和示例都表明构建 having 块适用于 <
、>
、<=
和 >=
等运算符。但是,=
抛出 SyntaxError
.
如果我使用 >
构建 having
语句,我会正确地得到 SQL:
query.having do sum(Sequel.case({{column => nil} => 1}, 0)) > count.function.* end
=> #<Sequel::Postgres::Dataset: "SELECT * FROM \"schema\".\"table\" GROUP BY \"id\" HAVING (sum((CASE WHEN (\"column\" IS NULL) THEN 1 ELSE 0 END)) > count(*))">
但是如果我将 >
更改为 =
,我会得到一个 SyntaxError
:
query.having do sum(Sequel.case({{column => nil} => 1}, 0)) = count.function.* end
SyntaxError: unexpected '=', expecting keyword_end
...nil} => 1}, 0)) = count.function.* end
... ^
SyntaxError: unexpected keyword_end, expecting end-of-input
... 1}, 0)) = count.function.* end
... ^~~
在这种情况下,什么是有效的 =
运算符?我在显示此用法的 Sequel 文档或规范中找不到提及。如果我错过了这个,抱歉,如果有人指出我该信息所在的位置,我很乐意重新阅读。
==
和 <=>
都会导致不希望的和不准确的 SQL,而且我也无法通过 <=
和 [=19= 的组合来破解它] 因为 having
块似乎没有将 &&
识别为多个条件(它只接受最后一个条件)。
query.having do sum(Sequel.case({{column => nil} => 1}, 0)) == count.function.* end
=> #<Sequel::Postgres::Dataset: "SELECT * FROM \"schema\".\"table\" GROUP BY \"id\" HAVING false">
query.having do sum(Sequel.case({{column => nil} => 1}, 0)) <=> count.function.* end
=> #<Sequel::Postgres::Dataset: "SELECT * FROM \"schema\".\"table\" GROUP BY \"id\" HAVING false">
query.having do sum(Sequel.case({{column => nil} => 1}, 0)) >= count.function.* && sum(Sequel.case({{column => nil} => 1}, 0)) <= count.function.* end
=> #<Sequel::Postgres::Dataset: "SELECT * FROM \"schema\".\"table\" GROUP BY \"id\" HAVING (sum((CASE WHEN (\"column\" IS NULL) THEN 1 ELSE 0 END)) <= count(*))">
根据这个 this discussion,这应该可以工作,将它包装在一个虚拟行中并使用 =>
的散列
query.having do
{ sum(Sequel.case({{column => nil} => 1}, 0)) => count.function.* }
end
或使用=~
query.having do
sum(Sequel.case({{column => nil} =~ 1}, 0)) == count.function.*
end
我正在尝试通过 Sequel 在 Ruby(Ruby 2.5.7,Sequel 5.28. 0), 但在使用相等比较时遇到问题。
所有文档和示例都表明构建 having 块适用于 <
、>
、<=
和 >=
等运算符。但是,=
抛出 SyntaxError
.
如果我使用 >
构建 having
语句,我会正确地得到 SQL:
query.having do sum(Sequel.case({{column => nil} => 1}, 0)) > count.function.* end
=> #<Sequel::Postgres::Dataset: "SELECT * FROM \"schema\".\"table\" GROUP BY \"id\" HAVING (sum((CASE WHEN (\"column\" IS NULL) THEN 1 ELSE 0 END)) > count(*))">
但是如果我将 >
更改为 =
,我会得到一个 SyntaxError
:
query.having do sum(Sequel.case({{column => nil} => 1}, 0)) = count.function.* end
SyntaxError: unexpected '=', expecting keyword_end
...nil} => 1}, 0)) = count.function.* end
... ^
SyntaxError: unexpected keyword_end, expecting end-of-input
... 1}, 0)) = count.function.* end
... ^~~
在这种情况下,什么是有效的 =
运算符?我在显示此用法的 Sequel 文档或规范中找不到提及。如果我错过了这个,抱歉,如果有人指出我该信息所在的位置,我很乐意重新阅读。
==
和 <=>
都会导致不希望的和不准确的 SQL,而且我也无法通过 <=
和 [=19= 的组合来破解它] 因为 having
块似乎没有将 &&
识别为多个条件(它只接受最后一个条件)。
query.having do sum(Sequel.case({{column => nil} => 1}, 0)) == count.function.* end
=> #<Sequel::Postgres::Dataset: "SELECT * FROM \"schema\".\"table\" GROUP BY \"id\" HAVING false">
query.having do sum(Sequel.case({{column => nil} => 1}, 0)) <=> count.function.* end
=> #<Sequel::Postgres::Dataset: "SELECT * FROM \"schema\".\"table\" GROUP BY \"id\" HAVING false">
query.having do sum(Sequel.case({{column => nil} => 1}, 0)) >= count.function.* && sum(Sequel.case({{column => nil} => 1}, 0)) <= count.function.* end
=> #<Sequel::Postgres::Dataset: "SELECT * FROM \"schema\".\"table\" GROUP BY \"id\" HAVING (sum((CASE WHEN (\"column\" IS NULL) THEN 1 ELSE 0 END)) <= count(*))">
根据这个 this discussion,这应该可以工作,将它包装在一个虚拟行中并使用 =>
query.having do
{ sum(Sequel.case({{column => nil} => 1}, 0)) => count.function.* }
end
或使用=~
query.having do
sum(Sequel.case({{column => nil} =~ 1}, 0)) == count.function.*
end