Jump to content

Cross table query help needed


ViperSBT

Recommended Posts

OK, I need to figure out how many people attended my events on different weekends....

 

For the people I have a table:

Table: attendance

Fields:
id
person
event

 

For the events table:

Table: events

Fields:
id
name
date

 

I can handle doing a query like:

SELECT a.person, e.name, e.date
FROM attendance a
JOIN events e ON a.event = e.id

 

So, that gives me a history of all the people and the event and date they have attended.

 

What I am looking for is a history of all the people that have attended multiple events on dates that are greater than 4 days apart from each other.  Can this be done in a query?

Link to comment
Share on other sites

yes.

 

SELECT a.person, e.name, e.date
FROM a_attendance a
   INNER JOIN a_events e ON a.event=e.id
WHERE a.person IN
   (SELECT a.person
      FROM a_attendance a
        INNER JOIN a_events e ON a.event=e.id
     GROUP BY a.person
     HAVING DATEDIFF(MAX(date),MIN(e.date)) > 4)
ORDER BY a.person, e.date

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.