在中合理的使用LEFTOUTERJOIN进行开发!

来源:互联网 发布:温润如玉的男人知乎 编辑:程序博客网 时间:2024/04/30 03:44
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

比如我们想对某人的消费项目进行汇总,对应以下两个表:Theme与ThemeDetail

Theme的记录为:
ThemeID(int)    ThemeName(varchar[10])
        1                        就餐
        2                        出差
        3                        乘车
        4                        其它

ThemeDetail的记录为:
DetailID(int)    ThemeID(int)    Price(money)
      1                    1                 12.5
      2                    1                    5
      3                    1                    6
      4                    2                   11
     5                    2                   17
     6                    3                    8

其中Theme中的ThemeID与ThemeDetail中的ThemeID是一对多的关系,对ThemeDetail表的理解如下:“就餐”费用为12.5+5+6=23.5元,“出差”费用为11+17=28元,“乘车”费用为8=8元,“其它”费用不存在,视为0处理,对应的SQL语句可以这样表示:

SELECTTOP100PERCENTdbo.Theme.ThemeName,ISNULL(SUM(dbo.ThemeDetail.Price),0)1
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>