java - 更新基于父子数据的排名

标签 java mysql

我有一个存储 user_idparent_user_id 的表。例如:

user_id    parent_user_id   calls      designation
---------------------------------------------------
1          0                 10        Tech Support
2          1                 5         Sr. Tech Support
3          2                 11        Tech Support
4          2                 12        Tech Support
5          4                 10        Tech Support

场景是,如果一个用户有 2 个 child ,每个 child 有 10 个电话,他将获得像高级技术支持这样的任命更改。如果他有 10 个这样的来电者,那就是经理。

到目前为止,我已经做到了这一点(java),

@Override
public boolean updateDesignation(int userId, int depth) {
    // check whether maximum depth is reached
    if (depth == 0)
        return false;
    depth--;

    int userIds = getIds(userId);//Will get parent_id

    String LOCAL_SQL = SQLconstants.getSQL("get-total-calls.sql");

    if(userIds>0) {
        int calls = jdbcTemplate.queryForObject(LOCAL_SQL, Integer.class, userIds);
            // I get 4's calls with which I need to see if I have 2 users with 10 calls each!
        updateDesignation(userIds, depth);
    }
    //updateRanks(userId, depth);       
    return true;
}

如果我传递 5 作为 user_id,传递 4 作为深度。它将继续直到 user_id 并更新值。它的工作原理是5->4, 4->2, 2->1。但我需要实现的是5->4,并检查4的 child 的调用。与 3, 2, 1 相同。我怎样才能做到这一点?请帮助我。

最佳答案

if(userIds>=0) {    // process 0 too, as it is a parent 
    /* execute this sql
       SELECT COUNT(*) FROM tablename WHERE parent_user_id=userIds AND calls>=10;
       then check if the returned value is >= 2 or other chekings...
       and update designations..
    */
    updateDesignation(userIds, depth);
}

这样您就不需要接到每个家长的电话。所以不再需要这一行:

int requests = jdbcTemplate.queryForObject(LOCAL_SQL, Integer.class, userIds);

关于java - 更新基于父子数据的排名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28256443/

相关文章:

mysql - 从 mysql 表中选择不同的记录

mysql - C* 迁移——将 1B+ 行表数据移动到新的模式表

sql - 有选择地减少数据库索引的策略

php - Mysql条件查询

java - 在java中查找括号字符串的有效性

java - "$"在Java/Android参数前有什么意义?

java - Spring Security 3.1.3 @Autowired 在使用 WebApplicationInitializer 时不起作用

java - 执行器服务缺少一些任务

java - JPA2.1/Hibernate 5.2 使用 Hibernate Tools 通过 ANT Maven 任务生成 ddl

python - 如何获取所有mysql元组结果并转换为json