Criando nova tabela baseando em outras 3 tabelas
Bom galera to cá com um problema que não ta mto legal...
Tenho a seguinte função absurda:
function manager_updateUsers_view(){
global $wpdb;
$users = $wpdb->get_results($wpdb->prepare("SELECT ID FROM wp_users WHERE 1 ORDER BY id ASC"));
foreach($users as $l){
$line = array('','','','','','','','','','','','','','');
$u = mysql_query("SELECT g.id as field_group_id, g.name as field_group_name, f.id as field_id, f.name as field_name, f.type as field_type, d.value as field_data, u.user_login, u.user_nicename, u.user_email, u.display_name, u.user_status, u.user_registered
FROM wp_bp_xprofile_groups g
LEFT JOIN wp_bp_xprofile_fields f ON g.id = f.group_id
INNER JOIN wp_bp_xprofile_data d ON f.id = d.field_id
LEFT JOIN wp_users u ON d.user_id = u.ID
WHERE d.value != '' AND d.user_id = $l->ID AND g.id = 1 AND f.type != 'radio'
GROUP BY f.id");
$i = 0;
while($o = mysql_fetch_object($u)){
if($o->field_id == 1){
$line[1] = $o->field_data;
}else if($o->field_id == 2){
if($i == 1){
$line[2] = $o->field_data;
}else {
for($c = $i; $c < 1; $c++){
$line[$i] = NULL;
}
$line[2] = $o->field_data;
$i = 1;
}
}else if($o->field_id == 7){
if($i == 2){
$line[3] = $o->field_data;
}else {
for($c = $i; $c < 2; $c++){
$line[$i] .= NULL;
}
$line[3] = $o->field_data;
$i = 2;
}
}else if($o->field_id == 8){
if($i == 3){
$line[4] = $o->field_data;
}else {
for($c = $i; $c < 3; $c++){
$line[$i] = NULL;
}
$line[4] = $o->field_data;
$i = 3;
}
}else if($o->field_id == 9){
if($i == 4){
$line[5] = $o->field_data;
}else {
for($c = $i; $c < 4; $c++){
$line[$i] = NULL;
}
$line[5] = $o->field_data;
$i = 4;
}
}else if($o->field_id == 14){
if($i == 5){
$line[6] = $o->field_data;
}else {
for($c = $i; $c < 5; $c++){
$line[$i] = NULL;
}
$line[6] = $o->field_data;
$i = 5;
}
}else if($o->field_id == 15){
if($i == 6){
$line[7] = $o->field_data;
}else {
for($c = $i; $c < 6; $c++){
$line[$i] = NULL;
}
$line[7] = $o->field_data;
$i = 6;
}
}else if($o->field_id == 70){
if($i == 7){
$line[8] = $o->field_data;
}else {
for($c = $i; $c < 6; $c++){
$line[$i] = NULL;
}
$line[8] = $o->field_data;
$i = 7;
}
}else if($o->field_id == 71){
if($i == 7){
$line[9] = $o->field_data;
}else {
for($c = $i; $c < 6; $c++){
$line[$i] = NULL;
}
$line[9] = $o->field_data;
$i = 8;
}
}
if($l->ID == 1){
for($i = 2; $i < 10; $i++)
$line[$i] = NULL;
}
$email = $o->user_email;
$name = $o->display_name;
if($o->user_status == 0)
$status = "Active";
else
$status = "Pending";
$created_at = $o->user_registered;
$i++;
}
$line[1] = ($line[1]) ? $line[1] : '';
$line[2] = ($line[2]) ? $line[2] : '';
$line[3] = ($line[3]) ? $line[3] : '';
$line[4] = ($line[4]) ? $line[4] : '';
$line[5] = ($line[5]) ? $line[5] : '';
$line[6] = ($line[6]) ? $line[6] : '';
$line[7] = ($line[7]) ? $line[7] : '';
$line[8] = ($line[8]) ? $line[8] : '';
$line[9] = ($line[9]) ? $line[9] : '';
$line[10] = $email;
$line[11] = $name;
$line[12] = $status;
$line[13] = $created_at;
$line[0] = $l->ID;
$count = $wpdb->get_var($wpdb->prepare("SELECT count(*) FROM wp_users_view WHERE user_id = $l->ID"));
if($count > 0){
$wpdb->update("wp_users_view", array('name'=>$line[1], "industry"=>$line[2], "jobtitle"=>$line[3], "company"=>$line[4], "company_size"=>$line[5], "city"=>$line[6], "country"=>$line[7], "province"=>$line[8], "lang"=>$line[9], "email"=>$line[10], "display_name"=>$line[11], "status"=>$line[12], "registered"=>$line[13]), array("user_id" => $line[0]));
}
else {
$wpdb->insert("wp_users_view", array("user_id"=>$line[0], "name"=>$line[1], "industry"=>$line[2], "jobtitle"=>$line[3], "company"=>$line[4], "company_size"=>$line[5], "city"=>$line[6], "country"=>$line[7], "province"=>$line[8], "lang"=>$line[9], "email"=>$line[10], "display_name"=>$line[11], "status"=>$line[12], "registered"=>$line[13]));
}
}
}
Como podem ver existe 1 loop dentro do loop... e isso não é legal alem de foder com o tempo de respota do BD
Agora minha pergunta é
Se eu rodar isso aqui:
SELECT g.id as field_group_id, g.name as field_group_name, f.id as field_id, f.name as field_name, f.type as field_type, d.value as field_data, u.user_login, u.user_nicename, u.user_email, u.display_name, u.user_status, u.user_registered
FROM wp_bp_xprofile_groups g
LEFT JOIN wp_bp_xprofile_fields f ON g.id = f.group_id
INNER JOIN wp_bp_xprofile_data d ON f.id = d.field_id
LEFT JOIN wp_users u ON d.user_id = u.ID
WHERE d.value != '' AND d.user_id = 1 AND g.id = 1 AND f.type != 'radio'
GROUP BY f.id
Colocando o ID = 1 retorna isso aqui:
/applications/core/interface/imageproxy/imageproxy.php?img=http://www.berseck.com/lalala/sql_result%2520001.png&key=e074b587e3222d62529792d35d1cddab6354a3fb1fdea7a87a34398b8359de85" alt="sql_result%20001.png" />
Como podem ver cada valor vem num row diferente...
Seria possivel só com 1 query pegar o valor Province, country etc num row só???
Eu to nisso a um tempão já tentei milhares de querys diferentes :(
Alguem tem alguma ideia?
Abraços
Discussão (3)
Carregando comentários...