SQL need to separate results by "before" and "after" date.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    SQL need to separate results by "before" and "after" date.

    Here's the situation: I have tables where relevant rows are...

    contact
    -- contact_id

    event
    -- event_id
    -- event_group_id

    groups
    -- group_id

    groupmember
    -- group_id
    -- contact_id

    attendance
    -- contact_id
    -- group_id
    -- event_id
    -- timestamp (current timestamp)


    On the page that shows the group info, I have a generated table that lists the members of that group from the database as shown here:

    PHP Code:
    // Query the database for group members
    $result mysql_query("SELECT contact.contact_id, contact.first, contact.last, contact.home_phone, contact.mobile_phone, contact.email, contact.address, contact.city, contact.state, contact.zip FROM contact INNER JOIN groupmember ON contact.contact_id = groupmember.contact_id WHERE groupmember.group_id = '$groupid' ORDER BY last");

    echo 
    '<table width="100%" border="0" cellpadding="2" cellspacing="5">
      <tr>
          <td width="5%"></td>
        <td width="10%"><div align="center"><strong><u>First Name</u></strong></div></td>
        <td width="15%"><div align="center"><strong><u>Last Name </u></strong></div></td>
        <td width="10%"><div align="center"><strong><u>Home Phone</u> </strong></div></td>
        <td width="15%"><div align="center"><strong><u>Mobile Phone </u></strong></div></td>
        <td width="20%"><div align="center"><strong><u>E-Mail</u></strong></div></td>
        <td width="25%"><div align="center"><strong><u>Address</u></strong></div></td>
        
      </tr>'
    ;
      

    // Generate group member results
    while($row=mysql_fetch_array($result))
    {

    echo 
    "<tr><td>";
                            
    // The delete record button
    echo '<form method="post" action="prcs.viewgroup.php" onsubmit="return confirm(';
    echo 
    "'Do you really want to remove this person from the group?'";
    echo 
    ');">
            <input name="viewgroup" type="hidden" id="viewgroup" value="'
    .$groupid.'">
            <input name="removemember" type="hidden" id="removemember" value="'
    .$row['contact_id'].'">
            <input name="delete" type="image" id="delete" src="images/x.png" title="Remove contact" style="border: none; background-color: #fff;">'
    ;    
                            
    echo 
    "                    </td>
                            <td><div align='center'><form action='prcs.viewcontact.php' method='post'><button type='submit' class='link' name='viewcontact' option value="
    .$row['contact_id']."><span>".$row['first']."</span></button></form></div></td>
                            <td><div align='center'><form action='prcs.viewcontact.php' method='post'><button type='submit' class='link' name='viewcontact' option value="
    .$row['contact_id']."><span>".$row['last']."</span></button></form></div></td>
                            <td><div align='center'>"
    .$row['home_phone']."</div></td>
                            <td><div align='center'>"
    .$row['mobile_phone']."</div></td>
                            <td><div align='center'><a href='mailto:"
    .$row['email']."'>".$row['email']."</a></div></td>
                            <td><div align='center'><a href='http://maps.google.com/?q="
    .$row['address'].", ".$row['city']." ".$row['state']."'>".$row['address'].", ".$row['city']." ".$row['state']."</a></div></td>
                            <td></td>
                          </tr>"
    ;
            } 
    What I would like to do is trim those results by checking the table named "attendance" and only showing those results that have attended an event sponsored by the group '$group' in the last 6 months. Then, after those results are shown, to generate another list at the bottom of the page showing those contacts who are 'inactive' because the last time they attended an event sponsored by '$group' was over 6 months ago. I would assume another WHERE statement would have to be appended to the SQL query but I don't even know if that can be done.? Can someone help me?

    #2
    umm

    The DATEADD function allows you to compute a date that is 6 months ago:

    PHP Code:
     DATEADD(m, -6current_timestamp
    Use this to compare your timestamp column:

    PHP Code:
    SELECT *
          
    FROM attendance
         WHERE timestamp 
    DATEADD(m, -6current_timestamp); 

    Comment


      #3
      I would call all from the database (with another join) and then filter timestamp>6 months into a separate array so you dont have to make an extra call to the database.
      eg:
      PHP Code:
      $badAttendance = array();
      $a 0;
      while(
      $row=mysql_fetch_array($result)) 

      if(
      $row['timestamp']>6months)
      {
          
      $badAttendance[$a] = $row['completeData'];
          
      $a++;
      }
      else
      {
          
      //normal table code here
      }
      }

      //followed by splitting the $badAttendance array into  another table code here 

      Comment

      Working...
      X