Hey All,
I'm really making a mess of this one!
In table accounts_contacts, there are multiple contact_id's related to a given account_id, and would like to count
the reationships and assign the count(*) value to accounts.mao (multi agent office)
For Starters, I have the following query ...
SELECT COUNT( * ) AS count, account_id
FROM accounts_contacts
GROUP BY account_id
HAVING count >1
which returns the desired counts for each duplicate account_id in accounts_contacts. All well and good,
but then I'm getting tangled up tring to translate that into an array, and ultimately a query to update the values
for accounts.mao where accounts.mao = the count if >1 else accounts.mao ='1'
Here's what I have so far, but I know it's far from right and I'm just plain stuck
$_count_mao = " SELECT COUNT(*)
AS count, account_id
FROM accounts_contacts
GROUP BY account_id
HAVING count > 1; "
$result = @mysql_query ($_count_mao) or die (Query failed - count_mao);
while ($row = mysql_fetch_assoc($result)) {
$_mao = $row[count]
$key = $row[account_id]
$_accounts[] = "('$key')";
}
foreach ($_accounts as $key => $value) {
$update_MAO = "UPDATE accounts SET mao = '$_mao' ";
}
else {$update_MAO = "UPDATE accounts SET mao = '1' ";
$result=@mysql_query($update_MAO);
}