返回包含多列唯一标识符的结果

时间:2016-12-07 17:51:23

标签: sql ms-access

尝试返回具有以下值的所有结果

数据:

Column1|Column2|Column3|ID
1      | 2     |1      |67
1      | 1     |1      |67
2      | 2     |2      |67
4      | 4     |1      |67

返回:

Column1|Column2|Column3|ID
1      | 1     |1      |67
2      | 2     |2      |67

PARAMATERS: 返回包含以下内容的所有数据:

1 in column1 AND 1 in column2 AND 1 in column3
2 in column1 AND 2 in column2 AND 2 in column3
3 in column1 AND 3 in column2 AND 3 in column3

谢谢!现在我只是进行多次查询并一起编译数据。

HAVING (((dbo_INVOICE.INV_TOTAL)>0) AND ((dbo_ITEMSORD.INVOICE_ID)="002")  AND
        ((dbo_PACKAGE.INVOICE_ID)="002") AND ((dbo_INVOICE.INVOICE_ID)="002"));

1 个答案:

答案 0 :(得分:2)

首先查看您的代码,而不是比较HAVING子句中的聚合,因此您可能真的希望在WHERE子句中使用这些条件。然后括号和AND / OR是你的朋友,这样你就可以在约束中建立优先权。所以对于你所描述的情况你可以写:

   WHERE
    (Column1 = 1 AND Column2 = 1 AND Column3 = 1)
    OR (Column1 = 2 AND Column2 = 2 AND Column3 = 2)
    OR (Column1 = 3 AND Column2 = 3 AND Column3 = 3)

如果你真的想要简化你可以测试A = B AND B = C而A是1,2或3.因为这意味着A = B = C并且它们在1和3之间。可以这样做:

WHERE
    Column1 = Column2 AND Column2 = Column3
    AND Column1 IN (1,2,3)

查看你特定的sql语句,这样的事情可能对你有用:

WHERE
    dbo_INVOICE.INV_TOTAL > 0
    AND dbo_ITEMSORD.INVOICE_ID = dbo_PACKAGE.INVOICE_ID
    AND dbo_PACKAGE.INVOICE_ID = dbo_INVOICE.INVOICE_ID
    AND dbo_ITEMSORD.INVOICE_ID IN ("001","002","003")

请注意,如果您只是希望所有列都相同,并且不关心这些值只是取出IN条件。