Solved

problem with not exists/ not in

Posted on 2008-09-30
2
168 Views
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 u.active=-2)
    and (
        u.id 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...
0
Comment
Question by:RunePerstrup
2 Comments
 
LVL 142

Accepted Solution

by:
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 u.active=-2)
    and (
        u.id not in (
        select userid from moneytrans m2
          WHERE  m2.LogDate > DATE_SUB(CURDATE(), INTERVAL 1 MONTH)
          AND m2.Type = 149
          AND m2.Accept = 1
          AND m2.Done = 1
        )
    )
;

Open in new window

0
 

Author Comment

by:RunePerstrup
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 :-)
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MS SQL Bulk load data error 5 34
sql server query? 6 28
combine an MS SQL string in Idera DM 9 31
Test a query 23 17
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

757 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

18 Experts available now in Live!

Get 1:1 Help Now