Welcome Guest, Not a member yet? Register   Sign In
Table Joining: Returning number of "no matches"
#1

[eluser]Hani A.[/eluser]
I need some help figuring out how to efficiently return a number of records from two joined tables.

I have two tables: Accounts and Purchases. Essentially, I'd like to know how many Accounts have NOT made any Purchases.

Here's where I start:

Code:
$this->db->join('Purchases', 'Purchases.accountID = Accounts.accountID', 'left');
$query = $this->db->get('individuals_purchased');
$result = $query->result_array();
echo ($result->num_rows());  // shows the number of purchases

What that returns is the number of collective purchases made. What I need is the number of accounts that have NO purchases. (Maybe the first question to ask is, how do I get the number of accounts that HAVE at least one purchase rather than the NUMBER of collective purchases across all accounts, such as in the example above.)

Thanks in advance for any guidance!
#2

[eluser]sqwk[/eluser]
This should only return the individuals that have no associated purchases: (LEFT OUTER join)

Code:
$this->db->from('individuals_purchased')
$this->db->join('Purchases', 'Purchases.accountID = Accounts.accountID', 'left outer');
$query = $this->db->get();
#3

[eluser]Hani A.[/eluser]
Thanks, squawk! Got it working! I need to brush up on my join types now. Smile




Theme © iAndrew 2016 - Forum software by © MyBB