例如:

F1   F2   F3   F4   F5   F6
语文 数学 英语 政治 物理 化学
数学 英语 政治 物理 化学 体育
地理 语文 数学 英语 政治 劳动
........
现有一条记录信息(例如为数组形式)
A(1)  A(2)  A(3)  A(4)  A(5)  A(6)
政治  物理  化学 体育  地理   语文 我该如何编写代码,实现选取表中的6个字段
的内容与这条记录信息重复N(可变)个的记录呢?
例如条件重复N=4,既得到结果为表中的第1,2条共二条。有位朋友提供:“
select * from 表 where (case when F1 in('政治','物理','化学','体育','地理','语文') then 1 else 0 end)+(case when F2 in('政治','物理','化学','体育','地理','语文') then 1 else 0 end)+(case when F3 in('政治','物理','化学','体育','地理','语文') then 1 else 0 end)+(case when F4 in('政治','物理','化学','体育','地理','语文') then 1 else 0 end)+(case when F5 in('政治','物理','化学','体育','地理','语文') then 1 else 0 end)+(case when F6 in('政治','物理','化学','体育','地理','语文') then 1 else 0 end)=4    

可以满足要求,但是只能在Microsoft SQL Server中运行,我该如何在VB的ADO中使用呢?是否还有其他的方法实现呢?请附源码及解释,谢谢!如能解决问题,分数不够的话还可再加。

解决方案 »

  1.   

    菜单"工程/引用/Microsoft Activex Data Object Library 2.0"后面为版本号
    Dim rs As New ADODB.Recordset
    Dim strSQL As StringstrSQL = "select * from 表 where (case when F1 in('政治','物理','化学','体育','地理','语文') then 1 else 0 end)+(case when F2 in('政治','物理','化学','体育','地理','语文') then 1 else 0 end)+(case when F3 in('政治','物理','化学','体育','地理','语文') then 1 else 0 end)+(case when F4 in('政治','物理','化学','体育','地理','语文') then 1 else 0 end)+(case when F5 in('政治','物理','化学','体育','地理','语文') then 1 else 0 end)+(case when F6 in('政治','物理','化学','体育','地理','语文') then 1 else 0 end)=4"rs.Open strSQL, cn, adOpenForwardOnly, adLockReadOnly
      

  2.   

    试试:
    select * from 表 where (iif F1 in('政治','物理','化学','体育','地理','语文'),1,0)+(iif F2 in('政治','物理','化学','体育','地理','语文'),1,0)+(iif F3 in('政治','物理','化学','体育','地理','语文'), 1, 0)+(iif F4 in('政治','物理','化学','体育','地理','语文'),1 ,0)+(iif F5 in('政治','物理','化学','体育','地理','语文') , 1,0)+(iif F6 in('政治','物理','化学','体育','地理','语文') ,1,0)=4
      

  3.   

    你的数据库表设计有问题,违反了线性原则。即使可以写出复杂的SQL来处理,也大大降低了效率。表
    F1   F2
    1    语文 
    1    数学
    1    英语
    1    政治
    1    物理
    1    化学
    2    数学
    2    英语
    2    政治
    2    物理
    2    化学
    2    体育
    3    地理
    3    语文
    3    数学
    3    英语
    3    政治
    3    劳动
    ......select F1, F2 from table where F2 in('政治','物理','化学','体育','地理','语文') 
    group by F1 having count(F1) = 4
      

  4.   

    如果你的数据库是sqlserver:
    菜单"工程/引用/Microsoft Activex Data Object Library 2.0"后面为版本号
    Dim rs As New ADODB.Recordset
    Dim strSQL As StringstrSQL = "select * from 表 where (case when F1 in('政治','物理','化学','体育','地理','语文') then 1 else 0 end)+(case when F2 in('政治','物理','化学','体育','地理','语文') then 1 else 0 end)+(case when F3 in('政治','物理','化学','体育','地理','语文') then 1 else 0 end)+(case when F4 in('政治','物理','化学','体育','地理','语文') then 1 else 0 end)+(case when F5 in('政治','物理','化学','体育','地理','语文') then 1 else 0 end)+(case when F6 in('政治','物理','化学','体育','地理','语文') then 1 else 0 end)=4 "rs.Open strSQL, cn, adOpenForwardOnly, adLockReadOnly如果是Access:
    strSql改为:
    strSQL="select * from 表 where (iif F1 in('政治','物理','化学','体育','地理','语文'),1,0)+(iif F2 in('政治','物理','化学','体育','地理','语文'),1,0)+(iif F3 in('政治','物理','化学','体育','地理','语文'), 1, 0)+(iif F4 in('政治','物理','化学','体育','地理','语文'),1 ,0)+(iif F5 in('政治','物理','化学','体育','地理','语文') , 1,0)+(iif F6 in('政治','物理','化学','体育','地理','语文') ,1,0)=4"
      

  5.   

    我用的是Access。

    select * from 表 where (iif F1 in('政治','物理','化学','体育','地理','语文'),1,0)+(iif F2 in('政治','物理','化学','体育','地理','语文'),1,0)+(iif F3 in('政治','物理','化学','体育','地理','语文'), 1, 0)+(iif F4 in('政治','物理','化学','体育','地理','语文'),1 ,0)+(iif F5 in('政治','物理','化学','体育','地理','语文') , 1,0)+(iif F6 in('政治','物理','化学','体育','地理','语文') ,1,0)=4

    可以在VB的ADO使用吗?我试试看。TO:yoki(小马哥)
    请问如何修改?为什么?谢谢。
      

  6.   

    Syntax error (comma) in query expression 
    select * from 表 where (iif F1 in('政治','物理','化学','体育','地理','语文'),1,0)+(iif F2 in('政治','物理','化学','体育','地理','语文'),1,0)+(iif F3 in('政治','物理','化学','体育','地理','语文'), 1, 0)+(iif F4 in('政治','物理','化学','体育','地理','语文'),1 ,0)+(iif F5 in('政治','物理','化学','体育','地理','语文') , 1,0)+(iif F6 in('政治','物理','化学','体育','地理','语文') ,1,0)=4
      

  7.   

    改成:
    select * from 表 
    where (iif (F1 in('政治','物理','化学','体育','地理','语文'),1,0))
    +(iif(F2 in('政治','物理','化学','体育','地理','语文'),1,0))
    +(iif(F3 in('政治','物理','化学','体育','地理','语文'), 1, 0))
    +(iif(F4 in('政治','物理','化学','体育','地理','语文'),1 ,0))
    +(iif(F5 in('政治','物理','化学','体育','地理','语文') , 1,0))
    +(iif(F6 in('政治','物理','化学','体育','地理','语文') ,1,0))=4
      

  8.   

    通过了,谢谢!
    另外,我想如果将“现有一条记录信息(例如为数组形式)
    A(1)  A(2)  A(3)  A(4)  A(5)  A(6)
    政治  物理  化学 体育  地理   语文 ”改为:
    “现有一条表记录(为表记录形式,如“表2”)
    s1    s2     s3    s4   s5     s6
    政治  物理  化学 体育  地理   语文
    体育  地理  语文 政治  物理   化学”此表2中记录有一条时(只有记录1),该如何写?有多条时又该如何写?
      

  9.   

    sql = "select t2.* from t2,s1 where (iif (t2.F1 in(s1.f1,s1.f2,s1.f3,s1.f4,s1.f5,s1.f6),1,0)) +(iif(t2.F2 in(s1.f1,s1.f2,s1.f3,s1.f4,s1.f5,s1.f6),1,0)) +(iif(t2.F3 in(s1.f1,s1.f2,s1.f3,s1.f4,s1.f5,s1.f6),1,0))+(iif(t2.F4 in(s1.f1,s1.f2,s1.f3,s1.f4,s1.f5,s1.f6),1,0))+(iif(t2.F5 in(s1.f1,s1.f2,s1.f3,s1.f4,s1.f5,s1.f6),1,0))+(iif(t2.F6 in(s1.f1,s1.f2,s1.f3,s1.f4,s1.f5,s1.f6),1,0))=4"
      

  10.   

    呵呵~,madxin(马欣)是搂主的导分马甲吧?