problem with not exists/ not in

Posted on 2008-09-30
Last Modified: 2012-05-05
I have posted this question in both the mysql forum and the mssql forum since this is a basic SQL question.

I have two tables: userinfo and moneytrans.

The userinfo contains (amongst others) a userID, a lastvisit date and a current balance.
The moneytrans contains information about moneytransactions - that is (amongst others) a transactiontype (type) and a transactiondate (logdate)
(Yes, i know the database has an unfortunate design, but it's hard to change that right now...)

I want all the users that have not been active for 3 months (userinfo.lastvisit is more than 3 months ago) but has a balance > 0 and has not had a transaction of transactiontype 149 for the past 30 days.

the simplest way for me to write that would be:

    SELECT distinct u.ID, u.money_balance
    FROM trendsalesdk.userinfo u
    WHERE u.LastVisit < DATE_SUB(CURDATE(), INTERVAL 3 Month)
    AND u.Money_Balance > 0.00
    AND (u.Active > 0 OR
    and ( not in (
        select userid from moneytrans m2
          WHERE  m2.LogDate <= DATE_SUB(CURDATE(), INTERVAL 30 Day)
          AND m2.Type = 149
          AND m2.Accept = 1
          AND m2.Done = 1

yes, i know I should rewrite that to a left join, but i have simplified my query to this . I think it should work but it doesn't.

when I execute this statement in phpmyadmin, I get almost all users (almost 400.000 users) - even though the first select statement alone returns a considerably smaller amount of users. That makes no sense to me.

1) Why does my query fail?
2) Can someone help me write a query that actually produces the job effectively? I'm getting slightly desperate...
Question by:RunePerstrup
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 143

Accepted Solution

Guy Hengel [angelIII / a3] earned 500 total points
ID: 22605187
what about this:
SELECT distinct u.ID, u.money_balance
    FROM trendsalesdk.userinfo u
    WHERE u.LastVisit < DATE_SUB(CURDATE(), INTERVAL 3 Month)
    AND u.Money_Balance > 0.00
    AND (u.Active > 0 OR
    and ( not in (
        select userid from moneytrans m2
          AND m2.Type = 149
          AND m2.Accept = 1
          AND m2.Done = 1

Open in new window


Author Comment

ID: 22605553
I seem to have used a "lesser than" instead of a "greater than". Silly me

As for PHPMyadmin, it still gives the wrong result, but in a command prompt i works fine. Yet another reason Never to use phpmyadmin.

Thanks :-)

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

730 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