表为下表:编号  编码    价格      价格2 
1      C        T        T 
2      B        E        G 
3      H        A        A 
4      Y        R        Y 
5      R        Y        S 
6      D        B        B 
7      E        G        E 
8      T        H        H 
前提条件:列名不知道,根据所给的两个条件查询出符合两个条件的一行如果我给的条件是两个"T" 那么我查出的结果是第一条.
如果我给的条件是两个"Y" 我查出的是第四条 "select top 1 1 from [" + row[2] + "] where [" + row[3] + "] like " + "'%" + str1 + "%' or [" + row[3] + "] like " + "'%" + str2 + "%'"我做出了一个条件的,两个条件就不行了,在线等待高手.....

解决方案 »

  1.   

    declare @cond1 varchar(10) --条件1
    declare @cond2 varchar(10) --条件2set @cond1 = '5'
    set @cond2 = '北京'select  *
    from table_name
    where (col1 + col2 + col3 + col4 ) like ('%'+ @cond1 + '%' + @cond2 + '%')
    or
      (col1 + col2 + col3 + col4 ) like ('%'+ @cond2 + '%' + @cond1 + '%')
      

  2.   


    --> 测试时间:2009-12-11 16:32:04
    --> 测试菜鸟:l8r
    --> 我的淘宝:《戒色坊》http://shop36766744.taobao.com/if object_id('[TB]') is not null drop table [TB]
    create table [TB]([编号] int,[编码] varchar(1),[价格] varchar(1),[价格2] varchar(1))
    insert [TB]
    select 1,'C','T','T' union all
    select 2,'B','E','G' union all
    select 3,'H','A','A' union all
    select 4,'Y','R','Y' union all
    select 5,'R','Y','S' union all
    select 6,'D','B','B' union all
    select 7,'E','G','E' union all
    select 8,'T','H','H'declare @s varchar(400)
    set @s='T'
    select * from TB where len(编码+价格+价格2)-len(replace(编码+价格+价格2,@s,''))=2
    /*编号          编码   价格   价格2  
    ----------- ---- ---- ---- 
    1           C    T    T(所影响的行数为 1 行)*/drop table [TB]
      

  3.   

    效率可就不好了
    可以考虑 union all 联合各种肯能的情况,效率要好点
      

  4.   


    这样的,首先我给俩条件比如两个"T"那我就在我的数据库里搜索内容为"T"的列,直到搜索到一行数据中其中有俩列是和我给的俩条件,就是我想要的结果了下面我给出怎么在一个不知道列名而查询列的代码 string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\桌面文件\远东超市数据库\Demo远东超市.mdb";
                        using (OleDbConnection connection = new OleDbConnection(connectionString))
                        {
                            connection.Open();
                            DataTable schemaTable = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Columns,new object[] { null, null, null, null });
    // 这里查出数据库所有的表                        foreach (DataRow row in schemaTable.Rows)//搜索表中的列内容
                            {
      

  5.   

    --> 测试时间:2009-12-11 16:32:04
    --> 测试菜鸟:l8r
    --> 我的淘宝:《戒色坊》http://shop36766744.taobao.com/if object_id('[TB]') is not null drop table [TB]
    create table [TB]([编号] int,[编码] varchar(1),[价格] varchar(1),[价格2] varchar(1))
    insert [TB]
    select 1,'C','T','T' union all
    select 2,'B','E','G' union all
    select 3,'H','A','A' union all
    select 4,'Y','R','Y' union all
    select 5,'R','Y','S' union all
    select 6,'D','B','B' union all
    select 7,'E','G','E' union all
    select 8,'T','H','H'declare @s varchar(10),@col varchar(200),@sql varchar(400)
    set @s='T'
    select @col=isnull(@col+'+','')+'rtrim('+name+')' from syscolumns where ID=object_id('TB')
    set @sql='select * from TB where len('+@col+')-len(replace('+@col+','''+@s+''',''''))=2'
    exec(@sql)/*
    编号          编码   价格   价格2  
    ----------- ---- ---- ---- 
    1           C    T    T*/--select * from TB where len(编码+价格+价格2)-len(replace(编码+价格+价格2,@s,''))=2drop table [TB]
      

  6.   

    select * from information_schema.columns where table_schema='tt' and table_ 
    name='newtt' and column_name='列名'; 
      

  7.   

    高手,能把你的语句帮我改成俩条件的吗?set @s='T'
    这里只有一个条件,帮我改成俩条件的,我试试
      

  8.   

    给个思路,原则是动态组成SQL语句,然后再运行1、获取该表所有字段名称,不过这点,你不知道有几个字段的情况下,存储的变量个数都不好定义
    2、其余就是组成查询条件了:定义个指针,连接字段与条件参数成字符串,所有的条件都是或者关系的
    3、运行SQL,返回结果
      

  9.   


    俩个"S" ,"Y"
    set @s='S'
    set @s='Y'
    这样搜索出来第五条谢谢高手
      

  10.   

    jiangshun你怎么不回答我的话啦快啊,我等着你呢手高
      

  11.   

    if object_id('[TB]') is not null drop table [TB]
    go
    create table [TB]([id] int,[a] varchar(1),[b] varchar(1),[c] varchar(1))
    insert [TB]
    select 1,'C','T','T' union all
    select 2,'B','E','G' union all
    select 3,'H','A','A' union all
    select 4,'Y','R','Y' union all
    select 5,'R','Y','S' union all
    select 6,'D','B','B' union all
    select 7,'E','G','E' union all
    select 8,'T','H','H'
    godeclare @s varchar(100)
    set @s='Y'
    /*
    结果
    4 Y R Y
    */
    set @s='T'
    /*
    结果
    1 C T T
    */
    set @s='S,Y'
    /*
    结果
    5 R Y S
    */
    set @s='G,E'
    /*
    2 B E G
    */select DISTINCT a.* from tb a
    inner join
    (
    select id  from
    (select b.k,idx,s from
    (select @s s,cast('<r>' + replace(case when charindex(',',@s)>0 then @s ELSE @s + ',' + @s end,',','</r><r>') + '</r>' as xml) k) a
    cross apply
    (
    select idx=row_number() over(order by getdate()),k= x.value('.','varchar(10)') from a.k.nodes('//r') as t(x)
    ) b
    ) c
    cross apply(select x = (select * from tb for xml path('r'),type)) a
    cross apply
    (
    select id=x.value('./id[1]','int'),idx1=x.value('xs:int(sql:column("c.idx"))','int'),v=x.value('count(./*[local-name(.)!="id" and text()=xs:string(sql:column("c.k"))])','int') from a.x.nodes('//r') as t(x)
    ) b
    where v>0
    group by id
    having count(distinct idx1)=2 and MAX(v) = case when charindex(',',@s)>0 THEN 1 else 2 end and count(distinct idx1) = 2 
    ) b
    on a.id=b.id
      

  12.   

    你太强了,可是我看不懂了,这样用起来这么长怎么用啊,
    那上面的那declare @s varchar(100)
    set @s='Y'
    /*
    结果
    4    Y    R    Y
    */
    set @s='T'
    /*
    结果
    1    C    T    T
    */
    set @s='S,Y'
    /*
    结果
    5    R    Y    S
    */
    set @s='G,E'
    /*
    2    B    E    G
    */
    这是条件怎么使用啊,我没年看懂喔!
      

  13.   


    declare @tmp1 varchar(2),@tmp2 varchar(2) set @tmp1='e' set @tmp2='g'
    select * from [tb] where [编码]+[价格]+[价格2] like '%'+@tmp1+@tmp2+'%' or
     [编码]+[价格2]+[价格] like '%'+@tmp1+@tmp2+'%'