Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP/SQL:组合查询结果的更快方法_Php_Sql_Arrays - Fatal编程技术网

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

PHP/SQL:组合查询结果的更快方法,php,sql,arrays,Php,Sql,Arrays,我正在连接来自两个SQL查询的数据,我想知道是否有一种更快的方法作为单个SQL查询进行连接,因为其中涉及很多循环。我有两个查询在“option_name”字段中查找不同的字符串值: 然后我创建两个数组: //Process the 1st SQL query data into an Array $result_array01 = array(); $j = 0; while($r = mysql_fetch_assoc($result01)) { if(!empty($r['op

我正在连接来自两个SQL查询的数据,我想知道是否有一种更快的方法作为单个SQL查询进行连接,因为其中涉及很多循环。我有两个查询在“option_name”字段中查找不同的字符串值:

然后我创建两个数组:

//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++;
    }   
}
最后,我将组合阵列:

//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++;
                }

            } 

         }

    }   
}

你为什么要在两个不同的查询中做什么? 在('val','val2')中使用mysql

但在你的情况下,使用或/和将帮助你,我一开始并不认为你想要合并同一张表。我删除我的答案并不是为了帮助你找到另一个解决方案

此外,还应使用DISTINCT以避免多个记录。
从表中选择不同的用户ID、选项值

//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++;
                }

            } 

         }

    }   
}
$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' ";