How to merge two mysql query results into one array?

You can use UNION operator to merge the result-set of two or more SELECT statements into one array.
The default behavior for UNION is that duplicate rows are removed from the result. UNION ALL does not remove duplicate.

There are few things need to be kept in mind.

A. SELECT statement within the UNION must have the same number of columns.
B. The field should have similar data types.
C. Fields in each SELECT statement must be in the same order if not make them.
D. If column have diffrent name make column another name by using an alias with “AS” kewords as given in the example

SELECT column1 AS alias_name FROM table

Here is the full example of UNION.

 PHP | 
 copy code |

  1.   $select_btemp_1 = "SELECT * FROM table1 WHERE enable='1' AND aid='1'";
  2.    $select_btemp_2 = "SELECT * FROM table1 WHERE enable='1' AND user_id='$user_id' ORDER BY  aid ASC";
  3.    $select_btemp =  $select_btemp_1." ".UNION." ".$select_btemp_2;
  4.    $btemp_query = mysql_query($select_btemp);
  5.    $btemp_count = mysql_num_rows($btemp_query);
  6.    if($btemp_count > 0)
  7.    {
  8. while($row_images = mysql_fetch_array($btemp_query))
  9. {
  10. echo $row_images[temp_desc];
  11. }
  12.    }