简体   繁体   English

PHP / SQL:组合查询结果的更快方法

[英]PHP/SQL: Faster Way to Combine Query Results

I'm joining data from two SQL queries and I'm wondering if there is a faster way to do this as a single SQL query because there is a lot of looping involved. 我正在连接来自两个SQL查询的数据,我想知道是否有更快的方法作为单个SQL查询来执行此操作,因为其中涉及很多循环。 I've got two queries that look for different string values in the "option_name" field: 我有两个查询,它们在“ option_name”字段中查找不同的字符串值:

 $sql01= "SELECT user_id, option_value FROM wp_wlm_user_options WHERE option_name = 'wpm_login_date' ORDER BY user_id";

 $sql02 = "SELECT user_id, option_value FROM wp_wlm_user_options WHERE option_name ='stripe_cust_id' ORDER BY user_id ";

Then I create two arrays: 然后创建两个数组:

//Process the 1st SQL query data into an Array

$result_array01 = array();
$j = 0;

while($r = mysql_fetch_assoc($result01)) {

    if(!empty($r['option_value'])){

            //User Id and Last Login

            $result_array01[$j]['user_id'] = $r['user_id'];
            $result_array01[$j]['last_login'] = $r['option_value'];
            $j++;
    }
}

//Process the 2nd SQL query data into an Array

$result_array02 = array();
$k = 0;

while($s = mysql_fetch_assoc($result02)) {

    if(!empty($s['option_value'])){

            //User Id and Stripe Customer Id

            $result_array02[$k]['user_id'] = $s['user_id'];
            $result_array02[$k]['cust_id'] = $s['option_value'];
            $k++;
    }   
}

And finally, I combine the arrays: 最后,我结合数组:

//Combine the SQL query data in single Array

$combined_array = array();
$l = 0;

foreach($result_array01 as $arr01){

    //  Check type
    if (is_array($arr01)) {

         //mgc_account_print("hello: " . $arr01['user_id'] . "\r\n");

         foreach($result_array02 as $arr02){

            //  Check type
            if (is_array($arr02)) {         


                //Check if User Id matches
                if($arr01['user_id'] == $arr02['user_id']){

                    //Create Array with User Id, Cust Id and Last Login

                    $combined_array[$l]['user_id'] =  $arr01['user_id'];
                    $combined_array[$l]['last_login'] =  $arr01['last_login'];
                    $combined_array[$l]['cust_id'] =  $arr02['cust_id'];
                    $l++;
                }

            } 

         }

    }   
}

Why you doing in two different queries? 为什么要在两个不同的查询中执行? Use mysql IN('val', 'val2'); 使用mysql IN('val','val2');

$sql01= "SELECT tbl1.user_id, tbl1.option_value FROM wp_wlm_user_options as tbl1 WHERE tbl1.option_name = 'wpm_login_date' 
union all 
SELECT tbl2.user_id, tbl2.option_value FROM wp_wlm_user_options as tbl2. WHERE tbl2.option_name ='stripe_cust_id' ";

But using OR/AND will your help you in your case , I didnt see at first that you want combined same table. 但是使用OR / AND将在您的情况下为您提供帮助,起初我没有看到您想要组合同一张表。 I didnt delete my answer to help you for another solution 我没有删除答案来帮助您寻求其他解决方案

Also you should use DISTINCT to avoid multiple records. 另外,您应该使用DISTINCT避免出现多个记录。 SELECT DISTINCT USER_ID, OPTION VALUE FROM TABLE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM