查询没有 return 个唯一结果
Query did not return a unique result
这是我写的查询:
select c.id, CONCAT(c.major_version, '.', c.minor_version) as versions
from event_ids c
where c_id in ('101') group by c_id, major_version, minor_version;
这是我从数据库中得到的输出:
id
versions
101
0.0
101
1.0
101
2.0
101
3.0
在我的应用程序中,我将此结果存储在
Map<List<String>, List<String>>
但它给出了一个错误,说“查询没有 return 一个唯一的结果”
如何存储这个结果?我可以使用哪种数据结构?
你应该像这样存储你的结果:
Map<String, HashSet<String>>
关键是 ID,列表包含版本。
当你迭代结果集时,你应该
- 检查密钥是否存在。
- 如果没有,添加一个空的字符串哈希集
- 在哈希集中添加版本
- 继续添加版本
试试下面的方法:
void test2(ResultSet resultSet) throws SQLException {
HashMap<String, List<String>> stringListHashMap = new HashMap<>();
while (resultSet.next()){
String id = resultSet.getString("id");
String version = resultSet.getString("versions");
if (stringListHashMap.containsKey(id)){ // check if the id is already available in the map
stringListHashMap.get(id).add(version); //if id is available get the list and add to the current version
} else {
// if not available create a new list
// add the current version to the list
// then put the id and list to the map
List<String> versions = new ArrayList<>();
versions.add(version);
stringListHashMap.put(id,versions);
}
}
}
这是我写的查询:
select c.id, CONCAT(c.major_version, '.', c.minor_version) as versions
from event_ids c
where c_id in ('101') group by c_id, major_version, minor_version;
这是我从数据库中得到的输出:
id | versions |
---|---|
101 | 0.0 |
101 | 1.0 |
101 | 2.0 |
101 | 3.0 |
在我的应用程序中,我将此结果存储在
Map<List<String>, List<String>>
但它给出了一个错误,说“查询没有 return 一个唯一的结果”
如何存储这个结果?我可以使用哪种数据结构?
你应该像这样存储你的结果:
Map<String, HashSet<String>>
关键是 ID,列表包含版本。
当你迭代结果集时,你应该
- 检查密钥是否存在。
- 如果没有,添加一个空的字符串哈希集
- 在哈希集中添加版本
- 继续添加版本
试试下面的方法:
void test2(ResultSet resultSet) throws SQLException {
HashMap<String, List<String>> stringListHashMap = new HashMap<>();
while (resultSet.next()){
String id = resultSet.getString("id");
String version = resultSet.getString("versions");
if (stringListHashMap.containsKey(id)){ // check if the id is already available in the map
stringListHashMap.get(id).add(version); //if id is available get the list and add to the current version
} else {
// if not available create a new list
// add the current version to the list
// then put the id and list to the map
List<String> versions = new ArrayList<>();
versions.add(version);
stringListHashMap.put(id,versions);
}
}
}