有没有办法聚合字符串值,如果它是唯一的或否则返回NULL.例如,给定表"STUFF":
Col_A | Col_B | Col_C | Col_D
1 | Foo | Bar | 6
2 | Foo | NoBar | 0
2 | Foo | Foo | 4
1 | Foo | Bar | 6
Run Code Online (Sandbox Code Playgroud)
这样的SQL
SELECT SUM(COL_A), STR_AGG_FUNC(COL_B), STR_AGG_FUNC(COL_C),SUM(COL_D) FROM STUFF
Run Code Online (Sandbox Code Playgroud)
会回来:
Col_A | Col_B | Col_C | Col_D
6 | Foo | NULL | 16
Run Code Online (Sandbox Code Playgroud) 我在膳食和食谱之间定义了has_and_belongs_to_many关联.在Meals创建表单中,我使用select来填充食谱.
<%= f.select :recipes, Recipe.all.collect { |x| [x.name, x.id]}, {}, :multiple => true %>
Run Code Online (Sandbox Code Playgroud)
但结果集的第一个值为零.
"recipes"=>["", "2", "7"]
Run Code Online (Sandbox Code Playgroud)
如何消除空/零值?