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

Started on the old forum in September 2012 · view the original in the archive · old links redirect here

2 replies · 684 views · started 14 years ago · 1 min read

3 participants
Budove Newbie · 6 posts #1

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:

// 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?

tkreturns Developer · 98 posts #2

umm

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

 DATEADD(m, -6, current_timestamp)

Use this to compare your timestamp column:

SELECT *
      FROM attendance
     WHERE timestamp > DATEADD(m, -6, current_timestamp);

ASK FOR HELP

something else Fellow · 2,296 posts #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:

$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

Write your reply now; next you create a free account (or sign in) and it is posted right away. Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close