0

我在查询的结果集中有两个整数列表,例如: List_1: 11,16,28... List_2 11,16,19 .. 如何在 Where 条件下比较这两个列表?条件是如果列表不同,则进行选择。

这是代码:

    SELECT cosechaAnterior.c_Fk_IdBoleta as 'BOLETA_P16', cosechaAnteriorDestino.c_Fk_IdBoleta as 'BOLETA_P17'
FROM Clt_CosechaAnterior cosechaAnterior
INNER JOIN  Clt_CosechaAnteriorDestino cosechaAnteriorDestino
ON cosechaAnterior.si_Fk_IdDesglose = cosechaAnteriorDestino.si_Fk_IdDesglose
INNER JOIN Blt_Boleta as boleta
ON cosechaAnterior.c_Fk_IdBoleta = boleta.c_Pk_IdBoleta
WHERE  --boleta.c_Pk_IdBoleta = 44990112--@id_boleta  
           (select si_Fk_IdDesglose
           from Clt_CosechaAnteriorDestino as cosechaAnteriorDestino
           where SUBSTRING(cosechaAnteriorDestino.c_Fk_IdBoleta,5,4) = '0112'
           AND cosechaAnteriorDestino.c_Fk_IdBoleta = 44990112)              (select si_Fk_IdDesglose
                                                                              from Clt_CosechaAnterior as cosechaAnterior
                                                                              where SUBSTRING(cosechaAnterior.c_Fk_IdBoleta,5,4)= '0112'
                                                                              AND cosechaAnterior.c_Fk_IdBoleta = 44990112)
4

3 回答 3

1

您希望通过连接而不是在 WHERE 子句中执行此操作。您的问题有点不清楚这两个列表是什么。所以这个答案给出了一个通用的解决方案。

假设列表采用两列格式,如 (, ),其中列表 for 由多个值组成。然后以下查询返回所有具有完全相同的值集的 id:

select list1.id
from (<subquery 1>) list1 full outer join
     (<subquery 2>) list2
     on list1.id = list2.id and
        list1.val = list2.val
group by list1.id
having max(case when list1.id is null then 1 else 0 end) = 0 and
       max(case when list2.id is null then 1 else 0 end) = 0

(此公式假设值中没有重复。)

这是一个完整的外部连接,并且只选择连接两侧没有 NULL 值的 id。当元素不匹配时,会在完全外连接上生成 NULL 值。

于 2012-06-04T21:47:59.607 回答
0

将每个列表视为一个文本块,并使用该函数测试两个文本值是否相等。

于 2012-06-04T22:08:24.563 回答
0

您可能可以使用except。如果list_2 之外的 list_1 的计数 > 0,那么您就知道这些列表是不同的。

于 2012-06-04T21:34:08.650 回答