从两个表中选择不同

时间:2018-07-22 13:40:54

标签: vb.net

我有一个表tblConsentQuestion,上面有问题

intID   nvcText      bitActive
17      Question1    True
18      Question2    True
19      Question3    False

和表格tblConsentData,其中包含每个客户的问题

intID   intCustomerID   bitConsent      intIDQuestion 
14      1               False           19
15      1               True            18

WHERE tblConsentQuestion.intID = tblConsentData.intIDQuestion

我想在VB.net数据集中进行检索:

  1. 所有tblConsentData都是特定客户的,即两个记录分别为intID = 1415

  2. bitActive = true tblConsentQuestion中的所有活动(WHERE tblConsentData.intIDQuestion <> tblConsentQuestion.intID)记录,在这种情况下,只有intID = 17条记录(除了两条记录)

输出应为:

Question3   False
Question2   True
Question1   Null

我尝试过类似的事情:

str = "Select tblConsentQuestion.intID, bitConsent, nvcText" & fungGetLangId() & " AS nvcqText " _
    & " From tblConsentData " _
    & " Left OUTER JOIN tblConsentQuestion " _
    & " On tblConsentData.intIDQuestion = tblConsentQuestion.intID " _
    & " where tblConsentData.intCustomerID = " & intCustomerID & " " _
    & " UNION ALL " _
    & " Select tblConsentQuestion.intID, -1, nvcText" & fungGetLangId() & " " _
    & " From tblConsentQuestion " _
    & " Left OUTER JOIN tblConsentData " _
    & " On tblConsentData.intIDQuestion = tblConsentQuestion.intID " _
    & " WHERE(tblConsentQuestion.bitActive = 'True') "

我收到了tblConsentQuestion中的所有活动记录,而我不应该收到intID = 18中存在的tblConsentData的记录。

1 个答案:

答案 0 :(得分:0)

bitActive的数据类型是什么?它是具有两个可能的字符串的文本字段:'True''False'吗?还是“访问是/否”字段?

如果是后者,则您的SQL不应检查字符串值,而应检查True / False常量(假设`fungGetLangId:

WHERE tblConsentQuestion.bitActive = True

或者,更好的是,您可以直接检查是/否字段:

WHERE tblConsentQuestion.bitActive