2009-03-26 13 views
0

基本的なPHPの問題で何か問題があり、誰かが私を助けることができるかどうかを知りたい。基本的には、2つのクエリの結果を結合し、キーに基づいて配列をマージし、両方のクエリに存在する1のキーを1の値に保持する必要があります。2つの配列をマージしてキーでソートする

例:「select * from table 1、table 2 where table1.id = table2.id」...この配列を作成します。

次に、 "select * from table3、table4 where table3.id2 = table4.id2" ..別の配列を作成します。

最後に、while($ res){各行を}で印刷します。

これをどう対処するかについてのアイデアはありますか?擬似コードは大いに感謝します。 idとの関係は、table1.id = table3.idですが、他のIDはクエリ内で提示されたテーブル間を結合するだけです。

答えて

3
<?php 

// Merge arrays keeping keys 
$new_array = array_merge($array1, $array2); 

// Sort by key 
ksort($new_array); 

?> 
2

2つの配列を別々に用意する必要がない場合は、SQLでより高速で少ないオーバーヘッドが必要です。

例:

"(select * from table1, table2 where table1.id = table2.id) 
    UNION ALL 
(select * from table3, table4 where table3.id2 = table4.id2)" 

これは、両方の配列に同じ構造を前提としません。 mySQLですが、構文は標準SQLであり、mySQL固有のものではありません。

か:

"Select * from ((select table1.id as id, * from table1, table2 where table1.id = table2.id) 
     UNION 
     (select table3.id as id, * from table3, table4 where table3.id2 = table4.id2)) as t 
ORDER BY t.id" 
関連する問題