php - convert multiple MySQL queries into a single query -


query 1...

$result = $wpdb->get_results("select wp_users.id,wp_users.user_login,wp_users.user_registered,wp_users.user_email,t.total,t.acc_nums wp_users left join wp_usermeta on ( wp_users.id = wp_usermeta.user_id ) left join (select count(*) total,user_id,group_concat(account_number) acc_nums `user_per_bank` group user_id) t on t.user_id=wp_users.id 1=1 , (    (  ( wp_usermeta.meta_key = 'wp_capabilities' , wp_usermeta.meta_value '%\"editor\"%' )   ) ) order user_registered desc", array_a); 

to combined query...

$out = $wpdb->get_results('select `user_id`, sum(`amount`) outstanding `assist_trans` left join `wp_users` on wp_users.id = assist_trans.user_id `status` in (0,2,4) group assist_trans.user_id order desc'); 

so "$out" variable can dropped , can use "$result" instead...

if i've understood question correctly, need make join first query second, , have outstanding column in result set. should work. it's untested, should point in right direction if i've made error.

the changes (besides formatting) new join , additional column in result set.

$result = $wpdb->get_results("select wp_users.id,                                      wp_users.user_login,                                      wp_users.user_registered,                                      wp_users.user_email,                                      t.total,                                      t.acc_nums,                                      o.outstanding                                 wp_users                                      left join wp_usermeta on ( wp_users.id = wp_usermeta.user_id )                                      left join (                                          select count(*) total,                                                 user_id,                                                 group_concat(account_number) acc_nums                                           `user_per_bank`                                        group user_id) t on t.user_id = wp_users.id                                      left join (                                          select `user_id`, sum(`amount`) outstanding                                            `assist_trans`                                                 left join `wp_users` on wp_users.id = assist_trans.user_id                                           `status` in (0,2,4)                                        group assist_trans.user_id) o on ( wp_users.id = o.user_id )                                1=1                                  , (                                         (                                           ( wp_usermeta.meta_key = 'wp_capabilities' , wp_usermeta.meta_value '%\"editor\"%' )                                        )                                      )                             order user_registered desc",             array_a); 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -