Joining two queries and out put as one

Hai folks

  • i want to list all the records for the previous month if the number of records only below 4.
    and
  • i want to list all the records for the current month if the number of records only below 4.

i want the output records to be printed in one table. so i want to make the out put of both queries in to a one array
so i can print them using a single while ( while($row=mysql_fetch_array($result)){ )statement.

$pm=date('m') -1;//previous month
$query="SELECT * from renewal_priority_print_approvals WHERE status='2' AND MONTH(dt)='$pm'"; //dt is the table field name contain the date (yyyy-mm-dd)
if ($result=mysql_query($query) or die (mysql_error()));
if(mysql_num_rows($result)<4){
 //somthing here
}

$pm=date('m');//current month
$query="SELECT * from renewal_priority_print_approvals WHERE status='2' AND MONTH(dt)='$pm'"; //dt is the table field name contain the date (yyyy-mm-dd)
if ($result=mysql_query($query) or die (mysql_error()));
if(mysql_num_rows($result)<4){
 //somthng here
}


how can i join the two out puts?

There are multiple ways to do it. However, with procedural spaghetti code like that it would probably be best optimized and elegant to use a union query as opposed to two separate queries. Look-up SQL unions if you are not familiar with them.

You should consider migrating over from using the mysql_* to either the mysqli_* or PDO for interacting with the database as the mysql_* extension is depreciated as of version 5.5 of PHP

Oh, Thanks buddy, i really forogot that western union command :smiley:


$query="SELECT * from renewal_priority_print_approvals WHERE status='2' AND WHERE MONTH(dt)='$pm'
        UNION ALL
        SELECT * from renewal_priority_print_approvals WHERE status='2' AND WHERE MONTH(dt)='$pm'"; 

Edited: but i have to deal with a condition :rolleyes:

oh, thanks for the note!!

Looking at it again you could just do this:

SELECT * from renewal_priority_print_approvals WHERE status=‘2’ AND WHERE MONTH(dt)=‘$pm_one’ OR MONTH(dt)=‘$pm_two’ "