如何从SQL查询中排除记录?

问题描述

给出以下SQL查询

<ul style="list-style-type: none;padding-left: 0px;" id="listitems">
  <li ><a href="#" id="mylink">my link 1</a></li>
</ul>
<button onclick="addrecepitem();" id="btnrecepajouter">Add line</button>

<script>
function addrecepitem(){
document.getElementById('listitems').innerHTML += '<li id="mylink">my link 2</li>';
}

$('#mylink').click(function(){
alert('I clicked on ');
}

</script>

结果是我获得了具有CODE列的更改值的记录的记录。 到目前为止一切顺利。

示例

更改

select t 
from tableA t,tableA t2 
where t.validAt = :validAt1 and t2.validAt = :validAt2 
and t.uniqueId = t2.uniqueId 
and nvl(t.code,'xNVLx') != nvl(t2.code,'xNVLx');

无变化:

CODE changed from  123  -> 456:  IS IN RESULT SET,PERFECT
CODE changed from  123  -> NULL: IS IN RESULT SET,PERFECT
CODE changed from  NULL -> 123:  IS IN RESULT SET,PERFECT

现在要添加另外两个特殊情况:

特殊情况:

CODE changed from  NULL -> NULL: NOT IN RESULT SET,PERFECT
CODE changed from  123  -> 123:  NOT IN RESULT SET,PERFECT

问题:是否存在一个优雅的简单sql排除项?

编辑:

我使用了@Plirkee提出的解决方案:

Special Case 1: CODE changed from  NULL -> 00: SHALL NOT BE RESULT SET
Special Case 2: CODE changed from  NULL -> 01: SHALL NOT BE RESULT SET

但是在这种逻辑上,从“ 00”到“ 01”的更改被视为有效,不应更改。

有什么想法吗?

解决方法

您可以使用decode函数

select t 
from tableA t,tableA t2 
where t.validAt = :validAt1 and t2.validAt = :validAt2 
and t.uniqueId = t2.uniqueId 
and nvl(t.code,'xNVLx') != nvl(decode(t2.code,'00','xNVLx','01',t2.code),'xNVLx');
,

您可以将逻辑实现为:

select . . .
from tableA t join
     tableA t2 
     on t.validAt = :validAt1 and t2.validAt = :validAt2 and
        t.uniqueId = t2.uniqueId 
where t.code <> t2.code or (t.code is not null and t.code is null);