Solved

"Not Greater than" operator for date

Posted on 2004-10-11
5
1,795 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Fore-Foreword Today (2016) Maxmind has a new approach to the distribution of its data sets.  This article may be obsolete.  Instead of using the examples here, have a look at the MaxMind API (https://www.maxmind.com/en/geolite2-developer-package). …
Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

863 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

24 Experts available now in Live!

Get 1:1 Help Now