SQL-所有客户端都应该收到至少特定的邮件

时间:2016-06-07 00:11:19

标签: sql

我需要检查我的#Personal表中没有客户端没有收到ABC电子邮件。

我正在使用自联接编写..这是正确的方法还是我应该简单地编写子查询?

我的表中包含客户ID和电子邮件发送详细信息。(如电子邮件代码,事件日期)

<div class="container" data-ng-controller="workshopController"> 
    <div>
        <ul>

            <!--want to add the class 'on' when user selects an item so we can style the selected item-->
            <li id='item_number0' class='item_row' ng-class="{'on': $index == selectedIndex}" data-ng-repeat="currentSound in sounds" data-ng-click="playmusic($index)">

            <span id='select_item{{currentSound.index}}'>

                <span class='item-column column-xxs-1'><i class='fa fa-play'></i></span>
                <span class='item-column column-xxs-5'>
                    <span class='item_col_split'>{{currentSound.title}}</span>
                    <span class='item_col_split'>{{currentSound.soundType}}</span>
                </span>         
                <span class='item-column column-xxs-2'>{{currentSound.index}}</span>
            </span>

            </li>

        </ul>

    </div>

        <audio id ='audio_player'>
            <source id='src_mp3' type='audio/mp3' src=''/>
            <source id='src_ogg' type='audio/ogg' src=''/>
            <object id='audio_object' type='audio/x-mpeg' width='200px' height='45px' >
                <param id = 'param_src' name='src'/>
                <param id = 'param_src' name='src'/>
                <param name='autoplay' value='false' />
                <param name='autostart' value='false' />
            </object>
        </audio>

</div><!--/controller-->

Select 
*
from #Personal a
join #Personal b
on a.id=b.id
where  b.emailcode<> 'ABC'

2 个答案:

答案 0 :(得分:0)

您可以使用group byhaving获取尚未收到此类电子邮件的人员列表:

select p.id
from #Personal p
group by p.id
having sum(case when p.emailcode = 'ABC' then 1 else 0 end) = 0;

having子句的另一种形式是:

having max(case when p.emailcode = 'ABC' then 'ABC' end) is null

答案 1 :(得分:0)

Select * 
FROM #Personal a
WHERE NOT EXISTS (SELECT 1 
                  FROM #Personal b 
                  WHERE a.id=b.id
                  AND b.emailcode = 'ABC')

如果ID列不可为空,您还可以使用

SELECT * FROM #Personal 
where id NOT IN (select id from #personal 
                 where emailcode = 'ABC')