我正在尝试为每个名称分配ID,例如。如果agentName是Bob,则为Bob分配ID,并且当插入新的数据行且agentName为Bob时,ID将保持不变。
这是sql表,有什么方法可以编码或使用SQL来查询它吗?
----------------------------------------------
ID AgentName Conversation |
---------------------------------------------|
1 Bob Hi |
2 Mary it should be 20-24 Jan |
3 Bob oops |
4 Jane yeah feb |
5 Jane my typo |
----------------------------------------------
答案 0 :(得分:1)
举个例子:
public class Prove {
public static HashMap<String, Integer> agentsId = new HashMap<String, Integer>();
public static Integer counter = 0;
public static void main(String[] args) {
//Add new agent
System.out.println("Bob " + addAgent("Bob"));
System.out.println("Mary " + addAgent("Mary"));
//Add old agent (return old id)
System.out.println("Bob " + addAgent("Bob"));
}
public static int addAgent(String name) {
if(agentsId.get(name)!=null) {
//do your work
return agentsId.get(name);
} else {
counter = counter + 1;
agentsId.put(name, counter);
//do your work
return agentsId.get(name);
}
}
}