UPDATE
Finally managed to work it out! Thanks for all the help from everyone. If you spot any potential errors or scope for improvement in my query please let me know.
SELECT *
FROM TBL_CAMPAIGNS C
INNER JOIN TBL_MEMBERS M
ON C.campaign_MemberId = M.members_Id
INNER JOIN TBL_CAMPAIGNS_CHARITIES CC
ON C.campaign_Key = CC.camchar_CampaignID
INNER JOIN TBL_CHARITIES CH
ON CC.camchar_CharityID = CH.cha_Key
LEFT OUTER JOIN (
select recip_Chosen, count(recip_CampaignId) as ChosenCount
from TBL_CAMPAIGNRECIPIENTS
WHERE recip_CampaignId = @campaign
group by recip_Chosen
) CRC
on CH.cha_Key = CRC.recip_Chosen
WHERE C.campaign_Key = @campaign
Thanks!!!
///////////////////
After some really useful advice i decided to implement orbMan' suggestion as follows;
SELECT *
FROM TBL_CAMPAIGNS C
INNER JOIN TBL_MEMBERS M
ON C.campaign_MemberId = M.members_Id
INNER JOIN TBL_CAMPAIGNS_CHARITIES CC
ON C.campaign_Key = CC.camchar_CampaignID
INNER JOIN TBL_CHARITIES CH
ON CC.camchar_CharityID = CH.cha_Key
WHERE C.campaign_Key = @campaign
This returns 1 row for each charity associated with a given campaign (as associated via TBL_Campaigns_Charities). However, i also have another table(TBL_CAMPAIGNRECIPIENTS CR) which details each person invited to take part in the campaign. On visiting the campaign page they can select one of the charities linked to the campaign.
Now i need to know how many people have chosen each of the associated charities(CR.recip_Chosen). Their details arent important. I just need to know how many people have selected each of the associated charities.
So something like;
COUNT CH.cha_Key, FROM CR WHERE CR.recip_Chosen = CH.cha_Key
but integrated into the statement above.
Thanks in advance.
ORIGINAL POST BELOW:
/ / / / / / / / / / / / / / / / / / /
Hi,
I need to gain data from across 3 tables. The first two are straight forward and are currently grabbed as;
SELECT * FROM TBL_CAMPAIGNS C
JOIN TBL_MEMBERS M
ON C.campaign_MemberId = M.members_Id
WHERE C.campaign_Key = @campaign
The table 'TBL_CAMPAIGNS' contains various columns, five of which hold an int. This int refers to the key of the 3rd table 'TBL_CHARITIES'. How do i return the data of the third table in combination with the above?
Ive created the following so far;
SELECT * FROM TBL_CAMPAIGNS C
JOIN TBL_MEMBERS M
ON C.campaign_MemberId = M.members_Id
JOIN TBL_CHARITIES CH
ON CH.cha_Key = C.campaign_Char1
WHERE C.campaign_Key = @campaign
But, as you can tell, that only returns C.campaign_Char1. What about C.campaign_Char2, C.campaign_Char3, C.campaign_Char4, C.campaign_Char5 ?????
I did try this;
SELECT * FROM TBL_CAMPAIGNS C
JOIN TBL_MEMBERS M
ON C.campaign_MemberId = M.members_Id
JOIN TBL_CHARITIES CH
ON CH.cha_Key = C.campaign_Char1
AND CH.cha_Key = C.campaign_Char2
AND CH.cha_Key = C.campaign_Char3
.......
WHERE C.campaign_Key = @campaign
But, of course this doesnt work!
Any suggestions / help?
Thanks in advance.