Sql 同一列上有多个“和”条件

Sql 同一列上有多个“和”条件,sql,asp.net,ms-access,Sql,Asp.net,Ms Access,我正在ASP.Net中使用Access 2007数据库,如果一个项遇到多个AND子句,我需要从表中提取数据 例如: Select * from tableA where tableA.column1 = myvalue1 and (tableA.column2 = myvalue2 and tableA.column2 = myvalue3) and子句周围的and括号是访问要求 myvalue1在column1中多次存在,因为column2存储column1值

我正在ASP.Net中使用Access 2007数据库,如果一个项遇到多个AND子句,我需要从表中提取数据

例如:

    Select * from tableA
    where tableA.column1 = myvalue1
    and (tableA.column2 = myvalue2
    and tableA.column2 = myvalue3)
and子句周围的and括号是访问要求

myvalue1在column1中多次存在,因为column2存储column1值的许多不同数据段

但是,我只希望结果显示满足所有column2搜索条件的column1值

例如,第1列包含员工姓氏,第2列包含员工的资格

第1栏中的一名员工可能拥有第2栏中的多项资格

使用“OR”子句搜索具有分配所需全部资格的员工,将为每个员工生成一个包含多行的表


所要求的是为持有每一项所需资格的每位员工提供一行

'Run through every Employee name
Do While qryEmployees.EOF = False
  'Set up a temp variable, MyQual, and set it equal to Empty
  MyQual = ""
  'Pull all qualifications from TableA for the current Employee
  Set rec = db.OpenRecordset ("Select Column1, column2 from TableA WHERE Column1 = '" & qryEmployees.Column1 & "'")
  'Looping through the qualifications
  Do While rec.EOF = False
    'Add the qualification to the MyQual string
    MyQual = MyQual & rec("Column2") & ", "
    rec.MoveNext
  Loop

  'Now that we have all the qualifications in a string, trim off the last ", "
  MyQual = left(MyQual, len(MyQual)-2)

  'Open up tblQualifications and fill it with the Employee and a string of all of their qualifications
  tblQualifications.AddNew
    tblQualifications(0) = qryEmployees.Column1
    tblQualifications(1) = MyQual
  tblQualifications.Update

'Move on to the next Employee and repeat the process
qryEmployees.MoveNext
Loop
假设员工A具有资格X、Y和Z。每次将资格添加到第2列时,员工的姓名都会出现在第1列的一行中,因此员工的姓名会出现在第1列中3次。假设员工B也有资格证书X、Y和Z,但员工C有资格证书W、X和Y。如果任务需要资格证书X、Y和Z,我不希望员工C出现在搜索结果中,因为员工C不会持有相关资格证书

'Run through every Employee name
Do While qryEmployees.EOF = False
  'Set up a temp variable, MyQual, and set it equal to Empty
  MyQual = ""
  'Pull all qualifications from TableA for the current Employee
  Set rec = db.OpenRecordset ("Select Column1, column2 from TableA WHERE Column1 = '" & qryEmployees.Column1 & "'")
  'Looping through the qualifications
  Do While rec.EOF = False
    'Add the qualification to the MyQual string
    MyQual = MyQual & rec("Column2") & ", "
    rec.MoveNext
  Loop

  'Now that we have all the qualifications in a string, trim off the last ", "
  MyQual = left(MyQual, len(MyQual)-2)

  'Open up tblQualifications and fill it with the Employee and a string of all of their qualifications
  tblQualifications.AddNew
    tblQualifications(0) = qryEmployees.Column1
    tblQualifications(1) = MyQual
  tblQualifications.Update

'Move on to the next Employee and repeat the process
qryEmployees.MoveNext
Loop
当我在上面的“select”命令中只使用一个“and”子句时,搜索工作正常,但在使用两个“and”子句时,不会返回任何内容,即使我知道第1列中的某些项同时满足“and”条件


“and”子句的串联中缺少了什么?

以下解决方案将起作用,但性能将受到影响。这将每人返回1行,我可以重写它以返回两行

Select * from tableA
where tableA.column1 = myvalue1
and (tableA.column2 = myvalue2
and DCount("column2", "TableA", "column1 = """ & column1 & """ AND column2 = """ & myvalue3 & """" ) <> 0)

基本问题是,没有GROUPBY或pivot子句的select查询一次只计算一行。我可能会对数据进行透视、连接,然后最终对其进行评估以获得更好的性能。但这更复杂,需要更多关于表结构和您想做什么的信息。

根据新信息,我将提交以下答案:

我的直觉是,你不能在一个简单的查询中做到这一点。这听起来很难理解,但我认为您需要创建一个查询,这是一个SELECT DISTINCT查询,该查询将为每个员工提取一条记录:

Select DISTINCT Column1 from tableA
将此查询称为qryEmployees

接下来需要创建一个新表。称之为TBL资格。它将包含2个字段;EName和均衡。我们将在一分钟内填补这一空缺

接下来,您需要遍历qryEmployees中的每个记录,并将员工姓名及其资格写入tblQualifications

'Run through every Employee name
Do While qryEmployees.EOF = False
  'Set up a temp variable, MyQual, and set it equal to Empty
  MyQual = ""
  'Pull all qualifications from TableA for the current Employee
  Set rec = db.OpenRecordset ("Select Column1, column2 from TableA WHERE Column1 = '" & qryEmployees.Column1 & "'")
  'Looping through the qualifications
  Do While rec.EOF = False
    'Add the qualification to the MyQual string
    MyQual = MyQual & rec("Column2") & ", "
    rec.MoveNext
  Loop

  'Now that we have all the qualifications in a string, trim off the last ", "
  MyQual = left(MyQual, len(MyQual)-2)

  'Open up tblQualifications and fill it with the Employee and a string of all of their qualifications
  tblQualifications.AddNew
    tblQualifications(0) = qryEmployees.Column1
    tblQualifications(1) = MyQual
  tblQualifications.Update

'Move on to the next Employee and repeat the process
qryEmployees.MoveNext
Loop
现在,您有一个表,其中一个字段中有员工姓名,下一个字段中有他的所有资格。如果在此表上使用LIKE运算符,则应得到结果

Select * from tblQualifications
where tblQualifications.EName = myvalue1
and (tblQualifications.EQualifications LIKE myvalue2
and tblQualifications.EQualifications LIKE myvalue3)

这并不漂亮,但需要对上面的代码进行一点运算,因为我正在从头开始编写,而且还没有经过测试。

谢谢Johnny,你的建议启发我编写了一种稍微不同的解决方案

我将“AND”改为“OR”,正如其他人建议的那样,如果任何员工具备任何资格,我会将其提取到表中

'Run through every Employee name
Do While qryEmployees.EOF = False
  'Set up a temp variable, MyQual, and set it equal to Empty
  MyQual = ""
  'Pull all qualifications from TableA for the current Employee
  Set rec = db.OpenRecordset ("Select Column1, column2 from TableA WHERE Column1 = '" & qryEmployees.Column1 & "'")
  'Looping through the qualifications
  Do While rec.EOF = False
    'Add the qualification to the MyQual string
    MyQual = MyQual & rec("Column2") & ", "
    rec.MoveNext
  Loop

  'Now that we have all the qualifications in a string, trim off the last ", "
  MyQual = left(MyQual, len(MyQual)-2)

  'Open up tblQualifications and fill it with the Employee and a string of all of their qualifications
  tblQualifications.AddNew
    tblQualifications(0) = qryEmployees.Column1
    tblQualifications(1) = MyQual
  tblQualifications.Update

'Move on to the next Employee and repeat the process
qryEmployees.MoveNext
Loop
结果是一个表格,其中列出了每一个具有这些资格之一的员工所需的所有资格

'Run through every Employee name
Do While qryEmployees.EOF = False
  'Set up a temp variable, MyQual, and set it equal to Empty
  MyQual = ""
  'Pull all qualifications from TableA for the current Employee
  Set rec = db.OpenRecordset ("Select Column1, column2 from TableA WHERE Column1 = '" & qryEmployees.Column1 & "'")
  'Looping through the qualifications
  Do While rec.EOF = False
    'Add the qualification to the MyQual string
    MyQual = MyQual & rec("Column2") & ", "
    rec.MoveNext
  Loop

  'Now that we have all the qualifications in a string, trim off the last ", "
  MyQual = left(MyQual, len(MyQual)-2)

  'Open up tblQualifications and fill it with the Employee and a string of all of their qualifications
  tblQualifications.AddNew
    tblQualifications(0) = qryEmployees.Column1
    tblQualifications(1) = MyQual
  tblQualifications.Update

'Move on to the next Employee and repeat the process
qryEmployees.MoveNext
Loop
然后,我在其他地方计算了该任务所需的资格数量,并将其与该员工是否具备所需资格数量进行了比较,如下所示:

    Dim objSearchTable As DataTable = DSSearch.Tables("Employees")

    Dim i As Integer

    If QualificationsCount > 0 Then

        For i = 0 To objSearchTable.rows.count - 1

            strEmployee = objSearchTable.Rows(i).Item("column1")

            Dim foundRows As DataRow() = objSearchTable.[Select]("column1 = '" & strEmployee & "'")

            Dim numberOfRecords As Integer = foundRows.count

            If numberOfRecords < QualificationsCount Then

                objSearchTable.Rows(i).Delete

            End If

        Next

    End If

    objSearchTable.AcceptChanges
如果资格计数小于所需数量,则此操作将从表中删除员工的每个实例

解决方案并不像从数据库中只提取正确的员工那样优雅


从数据库中提取每个员工也会有开销,但最终的结果是一个表,每个具有所有资格的员工只有一行,这正是我所需要的。

您确定不希望在第2和第3条之间使用OR吗?一个列值几乎不可能等于两个不同的值。我想看一个数据示例,其中column2=value1和column2=value2Can tableA.column2=myvalue2和tableA.column2=myvalue3在同一行中?如果没有,则需要或介于两者之间。更新您的问题添加适当的数据样本和预期结果添加更多详细信息和预期结果所需的是为持有每项所需资格的每位员工指定一行。将以下内容添加到您的SQL中:按列分组1按列排序1 Hanks Erik,我使用了Johnny答案的一个变体来获得有效的解决方案请参见页面上其他地方的我的答案。同时,我也会尝试你的方法 看看开销是否更低。。