MYSQL从表中将多个行从一个用户插入多个循环中的其他用户只插入最后一个结果

时间:2018-05-10 11:37:26

标签: php mysql

我试图将关系表中 client_id 67 的所有结果复制到users_groups表中的所有其他用户,因此id的99,111,130 必须在关系表中具有相同的结果id 67

所以所有查询都运行正常,更新工作正常,删除也是如此,但Insert不会将任何数据插入到关系表中以获取我需要的其他ID。

我搜索了stackoverflow,但没有找到任何可能在我的示例中起作用的解决方案。

我需要从

复制数据的用户ID 67的关系表和结果
signal_providers_relations TABLE

| id | client_id | provider_id | active_sp | subscription_sp | created_by | cr_datetime | ea_settings_active_sp | ea_settings_sp          | up_datetime | sp_risk_allocation | sp_max_trades |
|----|-----------|-------------|-----------|-----------------|------------|-------------|-----------------------|-------------------------|-------------|--------------------|---------------|
| 1  |  67       |  12         | Y         | 0000-00-00      | user123    | 2018-04-09  | Y                     | 0|1|0|1|1|2||1.0|0|0|1  | 2018-04-09  | 100                | 0             |
| 2  |  67       |  4          | N         | 0000-00-00      | user1      | 2018-04-10  | Y                     | 1|1|1|1|0|0|1|00|0|0|2  | 2018-04-10  | 120                | 1             | 
| 3  |  67       |  5          | Y         | 0000-00-00      | test23     | 2018-04-11  | N                     | 2|1|0|2|1|5|1|11|1|2|1  | 2018-04-11  | 95                 | 1             |

用户组表

users_groups TABLE

| id | user_id | group_id | user_role |
|----|---------|----------|-----------|
| 1  | 130     | 94       | 0         |
| 2  | 111     | 94       | 0         |
| 3  | 99      | 94       | 0         |
| 4  | 67      | 94       | 1         |

这是代码:

    $user_role = intval($_POST['user_role']);
    $userID = intval($_POST['user_id']);
    $groupID = intval($_POST['group_id']);
    $ea_settings = $_POST['ea_settings'];

    // get users from group to apply ea_settings
    $get_users = "SELECT a.group_name, b.user_id, b.user_role, b.group_id, c.email, c.ea_settings
            FROM user_groups a
            INNER JOIN users_groups b ON b.group_id = a.id
            INNER JOIN www_users c ON c.id = b.user_id
            WHERE a.id = '".$groupID."'";
    $pr = fetch_array($get_users);

    // get special user relations
    $query2 = "SELECT a.*, b.user_role
               FROM signal_providers_relations a
               INNER JOIN users_groups b ON a.client_id = b.user_id
               WHERE a.client_id = '".$userID."' AND b.user_role = '1'";
    $pr2 = fetch_array($query2);

    // get number of items from special user
    $query3 = "SELECT count(a.id) as cnt
                FROM signal_providers_relations a
                INNER JOIN users_groups b ON a.client_id = b.user_id
                WHERE a.client_id = '".$userID."' AND b.user_role = '1'";
    $pr3 = fetch_array($query3);

    // update all users where id = all id's from group, apply ea_settings to all id's from Special user 
    foreach ($pr as $key) {
        foreach ($pr2 as $r) {
            foreach ($pr3 as $s) {

                $user_id = intval($key['user_id']); // id of all users

                // update users
                $update_users = "UPDATE www_users SET ea_settings = '".$ea_settings."' WHERE id = '".$user_id."'";

                if (exec_query_confirm($update_users)) {

                    // first delete all user record in relations that are not special users, delete all record if user is have role = 0, and insert new data
                    if ($key['user_role'] == '0') {
                        $delete_relations = "DELETE FROM signal_providers_relations WHERE client_id = '".$user_id."'";
                        exec_query($delete_relations);

                        // loop through special user records and add to other users
                        for ($i=0; $i < $s['cnt']; $i++) { 

                            // $s['cnt'] = 10 = number of records in relations table from id 67

                            $insert_new_values = "INSERT INTO signal_providers_relations (client_id, provider_id, active_sp, subscription_sp, created_by, cr_datetime, ea_settings_active_sp, ea_settings_sp, up_datetime, sp_risk_allocation, sp_max_trades) 
                                       VALUES ('".$user_id."', '".$r['provider_id']."', '".$r['active_sp']."', '".$r['subscription_sp']."', '".$r['created_by']."', '".$r['cr_datetime']."', '".$r['ea_settings_active_sp']."', '".$r['ea_settings_sp']."', '".$r['up_datetime']."', '".$r['sp_risk_allocation']."', '".$r['sp_max_trades']."')";
                          exec_query($insert_new_values);

                        } 
                    }
                } else {
                    $err = 61;
                }
            } // foreach
        } // foreach
    } // foreach

}

基本上我需要这个

| id | client_id | provider_id | active_sp | subscription_sp | created_by | cr_datetime | ea_settings_active_sp | ea_settings_sp          | up_datetime | sp_risk_allocation | sp_max_trades |
|----|-----------|-------------|-----------|-----------------|------------|-------------|-----------------------|-------------------------|-------------|--------------------|---------------|
| 1  |  99       |  12         | Y         | 0000-00-00      | user123    | 2018-04-09  | Y                     | 0|1|0|1|1|2||1.0|0|0|1  | 2018-04-09  | 100                | 0             |
| 2  |  99       |  4          | N         | 0000-00-00      | user1      | 2018-04-10  | Y                     | 1|1|1|1|0|0|1|00|0|0|2  | 2018-04-10  | 120                | 1             | 
| 3  |  99       |  5          | Y         | 0000-00-00      | test23     | 2018-04-11  | N                     | 2|1|0|2|1|5|1|11|1|2|1  | 2018-04-11  | 95                 | 1             |

| id | client_id | provider_id | active_sp | subscription_sp | created_by | cr_datetime | ea_settings_active_sp | ea_settings_sp          | up_datetime | sp_risk_allocation | sp_max_trades |
|----|-----------|-------------|-----------|-----------------|------------|-------------|-----------------------|-------------------------|-------------|--------------------|---------------|
| 1  |  111      |  12         | Y         | 0000-00-00      | user123    | 2018-04-09  | Y                     | 0|1|0|1|1|2||1.0|0|0|1  | 2018-04-09  | 100                | 0             |
| 2  |  111      |  4          | N         | 0000-00-00      | user1      | 2018-04-10  | Y                     | 1|1|1|1|0|0|1|00|0|0|2  | 2018-04-10  | 120                | 1             | 
| 3  |  111      |  5          | Y         | 0000-00-00      | test23     | 2018-04-11  | N                     | 2|1|0|2|1|5|1|11|1|2|1  | 2018-04-11  | 95                 | 1             |

| id | client_id | provider_id | active_sp | subscription_sp | created_by | cr_datetime | ea_settings_active_sp | ea_settings_sp          | up_datetime | sp_risk_allocation | sp_max_trades |
|----|-----------|-------------|-----------|-----------------|------------|-------------|-----------------------|-------------------------|-------------|--------------------|---------------|
| 1  |  130      |  12         | Y         | 0000-00-00      | user123    | 2018-04-09  | Y                     | 0|1|0|1|1|2||1.0|0|0|1  | 2018-04-09  | 100                | 0             |
| 2  |  130      |  4          | N         | 0000-00-00      | user1      | 2018-04-10  | Y                     | 1|1|1|1|0|0|1|00|0|0|2  | 2018-04-10  | 120                | 1             | 
| 3  |  130      |  5          | Y         | 0000-00-00      | test23     | 2018-04-11  | N                     | 2|1|0|2|1|5|1|11|1|2|1  | 2018-04-11  | 95                 | 1             |

1 个答案:

答案 0 :(得分:1)

我认为您的主要问题是您实际上并不需要第三个查询或循环(foreach上的$pr2for上的$s['cnt']相同)。你应该这样做:

// update all users where id = all id's from group, apply ea_settings to all id's from Special user 
foreach ($pr as $key) {
    $user_id = intval($key['user_id']); // id of all users
    // update users
    $update_users = "UPDATE www_users SET ea_settings = '".$ea_settings."' WHERE id = '".$user_id."'";
    if (exec_query_confirm($update_users)) {
        // first delete all user record in relations that are not special users, delete all record if user is have role = 0, and insert new data
        if ($key['user_role'] == '0') {
            $delete_relations = "DELETE FROM signal_providers_relations WHERE client_id = '".$user_id."'";
            exec_query($delete_relations);
            // loop through special user records and add to other users
            foreach ($pr2 as $r) {
                $insert_new_values = "INSERT INTO signal_providers_relations (client_id, provider_id, active_sp, subscription_sp, created_by, cr_datetime, ea_settings_active_sp, ea_settings_sp, up_datetime, sp_risk_allocation, sp_max_trades) 
                                       VALUES ('".$user_id."', '".$r['provider_id']."', '".$r['active_sp']."', '".$r['subscription_sp']."', '".$r['created_by']."', '".$r['cr_datetime']."', '".$r['ea_settings_active_sp']."', '".$r['ea_settings_sp']."', '".$r['up_datetime']."', '".$r['sp_risk_allocation']."', '".$r['sp_max_trades']."')";
                exec_query($insert_new_values);
            } // foreach
        } else {
            $err = 61;
        }
    } // if
} // foreach