Solved

"Not Greater than" operator for date

Posted on 2004-10-11
5
1,788 Views
Last Modified: 2012-05-05
Did some research and some testing, and cannot find the logic to get my MySQL query to work right.

Basicly, I'm trying to create a statement that would work as a "Not Greater Than" operator.

Some background information:
I am creating a database that holds a list of events, credits, attendees. To work with the database, you first create an event, and then assign members who have attended.
With this information, I would like to generate several reports and statistics.

I already have done many queries that suit my needs, including a query that will show 'active' members - or "show all members who have attended an event 6 times in the past 6 months"

SELECT CONCAT(mem_name_amt, ' (',mem_name_last, ', ',mem_name_first, ')') FullName,  COUNT(*) attendance_count
FROM events,credits,members
WHERE events.evt_id=credits.evt_id
AND credits.mem_id=members.mem_id
AND evt_date>DATE_ADD(CURRENT_DATE(), INTERVAL -6 MONTH)
AND 'attendance_count'>6
GROUP BY members.mem_id

Now I'm stuck - really stuck, because the logic seems so simple.
I need a query that will show 'dead' members - or "show members who HAVE NOT attended an event in the past 6 months"
( Adding "AND NOT(AND evt_date>DATE_ADD(CURRENT_DATE(), INTERVAL -6 MONTH))" only seems to give me the inverse of what I want - or showing me members who have attended before '6 months ago')

Hopefully I have provided enough backgound information to get this question answered.

0
Comment
Question by:NeonDevil
  • 3
  • 2
5 Comments
 
LVL 3

Expert Comment

by:gnudiff
ID: 12284533
Umm. At the first glance, I would have said: why not simply change 'attendance_count' < 1 ?

But then I realized the inner join only includes members who have attended at least once.

So you need a left join to get all members, but as I am not sure how it ties together with credits table, this is incorrect, but should give the general direction:

SELECT mem_name, events.evt_id
FROM members
LEFT JOIN events ON events.member? = members.mem_id
WHERE evt_date>DATE_ADD(CURRENT_DATE(), INTERVAL -6 MONTH)
AND events.evt_id IS NULL

Maybe you will need to rewrite this as two queries:

- one to get all events of the past 6 months
- second to screen out all the members who have participated in any of the events of the first

Could be done with subquery easily, but since most MySQL versions still don't include it...
0
 
LVL 3

Accepted Solution

by:
gnudiff earned 125 total points
ID: 12284542
This would be the subquery version, which should work:

SELECT *
FROM members WHERE mem_id NOT IN (SELECT DISTINCT c.mem_id FROM credits c, events e WHERE events.evt_id=credits.evt_id
AND evt_date>DATE_ADD(CURRENT_DATE(), INTERVAL -6 MONTH) )
0
 
LVL 1

Author Comment

by:NeonDevil
ID: 12287247
gnudiff,

Thanks for the prompt reply.

Unfortunately, I'm working on version 4.0.2, so I am not able to use any subqueries.

You're definitely pointing me in the right direction. I'll have to play around with it until I get working.

Here's a little insight on my database structure....
Basicly, the credits table provides the many-to-many relationships between the event and members. The credits table also records the level of activity of each member per event.

Here's the rundown of the fields in tables that are required for the query:
events.evt_id
events.evt_date
members.mem_id
members.mem_name_amt
credits.evt_id
credits.mem_id
0
 
LVL 3

Expert Comment

by:gnudiff
ID: 12287556
Without subqueries, I am afraid I really don't see at the moment how it is possible in MySQL via single query.

You can do it via temporary tables though, in 2 queries:

CREATE TEMPORARY TABLE active_6months AS
SELECT DISTINCT c.mem_id FROM credits c, events e WHERE events.evt_id=credits.evt_id
AND evt_date>DATE_ADD(CURRENT_DATE(), INTERVAL -6 MONTH)

SELECT mem_name FROM members m LEFT JOIN active_6months act ON act.mem_id=m.mem_id
WHERE act.mem_id IS NULL;

0
 
LVL 1

Author Comment

by:NeonDevil
ID: 12289334
I think it's better to bite the bullet and update to MySQL 4.1, as this particular database is mostly used for learning/experimental purposes. I've been procrastinating about switching, now I have a perfectly good reason.

However, I was unable to update the WinNT server to 4.1 (BSODs) here at work - so it's going to stay at 4.0. But this is a whole other problem alltogether.

I tested your subquery, and its primo!
I have enough information to suit it to my needs.
Thanx for the input.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now