我在 Rails 中有一个相当长的 SQL 查询:
Region.includes(:answers, answers: :choice)
.where('choices.id IS NULL OR choices.id = ?', 28)
.where('answers.id IS NULL OR answers.question_id = ?', 14)
.group("regions.region","choices.choice").count("answers")
这给了我所有区域的哈希值,包括选择名称和答案计数,即使计数为零。这是输出:
{["East", nil]=>0, ["East Midlands", nil]=>0, ["London", nil]=>0, ["North East", nil]=>0, ["North West", nil]=>0, ["Northern Ireland", nil]=>0, ["Rest of World", "No"]=>3, ["Scotland", nil]=>0, ["South East", nil]=>0, ["South West", nil]=>0, ["Wales", nil]=>0, ["West Midlands", nil]=>0, ["Yorkshire and the Humber", nil]=>0}
目前唯一有答案的地区是“世界其他地区”,它显示选项名称“否”。
所有其他项目都显示“nil”作为选项名称 (choices.choice)。如何更改查询以输出选择名称“否”而不是 nil,即使没有与之关联的答案?
谢谢