2 messages in com.mysql.lists.mysqlRe: One to many meetting specific con...
FromSent OnAttachments
Scott Haneda25 May 2006 21:55 
Jay Pipes26 May 2006 07:01 
Subject:Re: One to many meetting specific conditions
From:Jay Pipes (ja@mysql.com)
Date:05/26/2006 07:01:59 AM
List:com.mysql.lists.mysql

Scott Haneda wrote:

4.0.18-standard-log

I have a very basic one to many relationship, accounts and transactions.

There is only one account per users, but of course, there can be x transactions. These are once a month charges for billing.

I need to be able to select all accounts where next_charge_date <= NOW() That's the easy part, I get all the records I want. However, some of those get charges through one gateway, and some get charged through another.

The transaction table has a field called merchant, lets say it can be bankA or bankB.

So, I need a list of accounts, where none of its "many" transaction records has the merchant bankA.

Wouldn't something like this suit your needs?

SELECT a.account_id FROM accounts a LEFT JOIN transactions t ON a.account_id = t.account_id AND t.next_charge_date <= NOW() AND t.merchant != 'bankA' GROUP BY a.account_id;

Are You MySQL Certified? http://www.mysql.com/certification Got Cluster? http://www.mysql.com/cluster