SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    966
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    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.

    PHP Code:
    $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?

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,182
    Mentioned
    16 Post(s)
    Tagged
    4 Thread(s)
    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.
    The only code I hate more than my own is everyone else's.

  3. #3
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,067
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    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
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  4. #4
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    966
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by oddz View Post
    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.
    Oh, Thanks buddy, i really forogot that western union command

    Code:
    $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

  5. #5
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    966
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SpacePhoenix View Post
    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 for the note!!

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,182
    Mentioned
    16 Post(s)
    Tagged
    4 Thread(s)
    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' "
    The only code I hate more than my own is everyone else's.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •