?
Solved

"Not Greater than" operator for date

Posted on 2004-10-11
5
Medium Priority
?
1,806 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 500 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month14 days, 11 hours left to enroll

839 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