我有两张 table
账户表
id INT, username TEXT, password TEXT, proxy_id INT, enabled BOOLEAN
代理表
id INT, proxy_ip TEXT, proxy_port INT
我会有一个循环,一次处理一个帐户。我希望将最少使用的代理分配给 table.proxy_id。
例如,如果我们在代理表中有 2 个代理,在帐户表中有 5 个帐户
1 10.0.0.1 4000
2 10.0.0.1 4001
我们的账户
1 david password 2 enabled
2 mark password 1 enabled
3 jessica password 1 enabled
4 ashley password NULL enabled
5 allan password NULL enabled
6 james password 2 disabled
我的程序将在 Java 中循环遍历所有已启用的帐户,它将已启用帐户中使用最少的代理分配给该帐户。在上面的示例中,David、mark 和 Jessica 已经有一个代理集。因此循环将通过 Ashley,并且需要将 ID 为 2 的代理分配给 Ashley,因为它使用最少。对于 Allan 代理 1 或 2 可以分配,因为它在任何情况下都是最少使用的。 James 应该被忽略,因为他的帐户未启用。
我希望我的问题很清楚。我认为这需要在两个查询中完成?
最佳答案
我为此使用了 Java 和 SQL。但它使它更容易阅读。我已经测试过了,它可以工作
public void setLeastUsedProxy() throws Exception {
Database db = new Database();
ArrayList<Integer> allProxies = db.getAllProxies();
ArrayList<Integer> allAssignedProxies = db.getAssignedProxies();
ArrayList<Integer> unusedProxies = allProxies;
unusedProxies.removeAll(allAssignedProxies);
// assign the unused proxy
if (unusedProxies.size() > 0) {
} else {
Integer leastUsedProxy = db.getLeastUsedProxy();
System.out.println(leastUsedProxy);
}
}
下面是你的 SQL 方法
public ArrayList<Integer> getAssignedProxies() throws Exception {
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
ArrayList<Integer> list = new ArrayList<Integer>();
try {
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection(url, props);
pst = conn
.prepareStatement("select distinct(proxy_id) from account where proxy_id IS NOT null AND enabled = true");
rs = pst.executeQuery();
while (rs.next()) {
list.add(rs.getInt("proxy_id"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (pst != null) {
pst.close();
}
if (rs != null) {
rs.close();
}
if (conn != null) {
conn.close();
}
}
return list;
}
public ArrayList<Integer> getAllProxies() throws Exception {
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
ArrayList<Integer> list = new ArrayList<Integer>();
try {
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection(url, props);
pst = conn.prepareStatement("select distinct(id) from proxy");
rs = pst.executeQuery();
while (rs.next()) {
list.add(rs.getInt("id"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (pst != null) {
pst.close();
}
if (rs != null) {
rs.close();
}
if (conn != null) {
conn.close();
}
}
return list;
}
public Integer getLeastUsedProxy() throws Exception {
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
Integer leastUsedProxy = null;
try {
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection(url, props);
pst = conn
.prepareStatement("SELECT proxy_id, count(proxy_id) FROM account GROUP by proxy_id ORDER BY count LIMIT 1");
rs = pst.executeQuery();
while (rs.next()) {
leastUsedProxy = rs.getInt("proxy_id");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (pst != null) {
pst.close();
}
if (rs != null) {
rs.close();
}
if (conn != null) {
conn.close();
}
}
return leastUsedProxy;
}
关于java - 在另一个表中获取最少使用的 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49867038/