需要SQL语句帮助

时间:2009-08-20 13:38:22

标签: asp.net sql sql-server-2005 tsql stored-procedures

更新

终于成功了!感谢大家的帮助。如果您发现我的查询中存在任何潜在错误或改进范围,请告知我们。

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

感谢!!!

///////////////////

经过一些非常有用的建议后,我决定按照以下方式实施orbMan的建议;

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

这为与给定广告系列相关联的每个慈善机构返回1行(通过TBL_Campaigns_Charities关联)。但是,我还有另一个表格(TBL_CAMPAIGNRECIPIENTS CR),详细说明了受邀参与活动的每个人。在访问活动页面时,他们可以选择与活动相关联的慈善机构之一。

现在我需要知道有多少人选择了每个相关的慈善机构(CR.recip_Chosen)。他们的细节不重要。我只需要知道有多少人选择了每个相关的慈善机构。

就像这样;

COUNT CH.cha_Key, FROM CR WHERE CR.recip_Chosen = CH.cha_Key

但已整合到上述声明中。

提前致谢。

下面的原始帖子:

/ / / / / / / / / / / / / / / / /

您好,

我需要从3个表中获取数据。前两个是直截了当的,现在被抓住了;

 SELECT * FROM TBL_CAMPAIGNS C
 JOIN TBL_MEMBERS M
 ON C.campaign_MemberId = M.members_Id
 WHERE C.campaign_Key = @campaign

表'TBL_CAMPAIGNS'包含各种列,其中五列包含int。这个int指的是第3个表'TBL_CHARITIES'的键。如何结合上述内容返回第三个表的数据?

到目前为止,我创建了以下内容;

 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

但是,正如你所知,只返回C.campaign_Char1。怎么样C.campaign_Char2,C.campaign_Char3,C.campaign_Char4,C.campaign_Char5 ?????

我确实试过了;

 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

但是,当然这不起作用!

有任何建议/帮助吗?

提前致谢。

3 个答案:

答案 0 :(得分:3)

这是一个非规范化设计,这就是你查询它的难度。如果(而不是列campaign_Char1到5)你在TBL_CAMPAIGNS和TBL_CHARITIES之间有一个多对多的表会更容易。例如,TBL_CAMPAIGNS_CHARITIES。这将包含Campaign ID和CharityID。

然后你的查询将是:

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.CampaignID
INNER JOIN TBL_CHARITIES CH
    ON CC.CharityID = CH.cha_Key
WHERE C.campaign_Key = @campaign

<强>更新

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(*) as ChosenCount
    from TBL_CAMPAIGNRECIPIENTS 
    group by recip_Chosen
) CRC
on CH.cha_Key = CRC.recip_Chosen
WHERE C.campaign_Key = @campaign

答案 1 :(得分:2)

首先想到的是,您必须再次为要提供的每个引用加入TBL_CHARITIES表。

SELECT * FROM TBL_CAMPAIGNS C
 JOIN TBL_MEMBERS M
 ON C.campaign_MemberId = M.members_Id
 JOIN TBL_CHARITIES CH1
 ON CH1.cha_Key = C.campaign_Char1
 JOIN TBL_CHARITIES CH2
 ON CH2.cha_Key = C.campaign_Char2
 JOIN TBL_CHARITIES CH3
 ON CH3.cha_Key = C.campaign_Char3
 JOIN TBL_CHARITIES CH4
 ON CH4.cha_Key = C.campaign_Char4
 JOIN TBL_CHARITIES CH5
 ON CH5.cha_Key = C.campaign_Char5
 WHERE C.campaign_Key = @campaign

我确信有人有更好的解决方案。

答案 2 :(得分:1)

在@OrbMan上展开一点,运行以下SQL来演示它是如何发挥作用的。它应该显示表的外观,包括多对多@ camp2char表。

set nocount on
DECLARE @camp TABLE (
    ID int,
    ID2 int,
    primary key (id)
)
DECLARE @memb table (
ID int NOT NULL,
primary key (id)
)
DECLARE @chars table (
ID int NOT NULL,
primary key (id)
)
DECLARE @camp2char table (
ID1 int NOT NULL,
ID3 int NOT NULL
)
insert into @memb (id) values(100);
insert into @memb (id) values(200);
insert into @memb (id) values(300);
insert into @chars (id) VALUES(1000);
insert into @chars (id) VALUES(2000);
insert into @chars (id) VALUES(3000);
insert into @chars (id) VALUES(4000);
insert into @chars (id) VALUES(5000);
insert into @camp (ID,ID2) VALUES(1,100);
insert into @camp (ID,ID2) VALUES(2,300);
insert into @camp2char (ID1,ID3) VALUES(1,1000);
insert into @camp2char (ID1,ID3) VALUES(1,2000);
insert into @camp2char (ID1,ID3) VALUES(1,3000);
insert into @camp2char (ID1,ID3) VALUES(1,5000);
insert into @camp2char (ID1,ID3) VALUES(2,2000);

PRINT '@camp';
select * from @camp;
PRINT '@memb';
select * from @memb;
PRINT '@chars';
select * from @chars;
PRINT '@camp2char';
select * from @camp2char;

select c.ID 'camp.id', m.ID 'memb.id', ch.id 'char.id' from @camp c
inner join @memb m
on c.id2 = m.id
inner join @camp2char c2ch
on c.id = c2ch.id1
inner join @chars ch
on c2ch.id3 = ch.id
where c.id=1

这种方法的一个结果是,您的结果行数不会超过一个结果行,而是等于与目标广告系列匹配的匹配慈善行数。

这样做的好处是,您可以将任意数量的慈善机构与任意数量的广告系列相关联。

此外,如果每个广告系列中有多个成员,则需要以相同的方式对其进行规范化(例如,使用camp2memb表)。

脚本在SQL SVR 2005上运行

@camp
ID          ID2
----------- -----------
1           100
2           300

@memb
ID
-----------
100
200
300

@chars
ID
-----------
1000
2000
3000
4000
5000

@camp2char
ID1         ID3
----------- -----------
1           1000
1           2000
1           3000
1           5000
2           2000

camp.id     memb.id     char.id
----------- ----------- -----------
1           100         1000
1           100         2000
1           100         3000
1           100         5000