select * from tablename where code in (select Code from tablename where name='asd' and info='INFO' and code like '001*' )

解决方案 »

  1.   

    select * from table1 where Id=(select id from table2)
      

  2.   

    我是想让 符合 code like '001*'的记录成为一个集合 A,然后再从A 里选择
    name='asd' and info='info'的记录
      

  3.   

    select * form 表名 where "字段" in (select * from "表名" )
      

  4.   

    select * from tablename where name='asd' and info='INFO' and code in (select code from 表名 where 条件) 
      

  5.   

    select a.* from ( select * from table where code like '001*') a
    where a.name='asd' and info='info'
      

  6.   

    select a.* from (select * from table where code like '001*') a
    where a.name='asd' and a.info='INFO'
    其实结果应该和以上各位是一样的
      

  7.   

    如果是一张表用不着子查询:
    select * from tablename where name='asd' and info='INFO' and code like '001*', 不好吗?
    如果两张表,最好也不用IN子查询。
      

  8.   

    select a.* from tablename a,(Select * from tablename where code like '001*') b where where a.name='asd' and a.info='INFO' and a.code=b.code
      

  9.   

    select a.* from tablename a,(Select * from tablename where code like '001*') b where a.name='asd' and a.info='INFO' and a.code=b.code
      

  10.   

    若是SQL SERVER,将 Code Like '001*'改为 Code Like '001%'就可以,不用写子查询。
      

  11.   

    只有一个表。
    我原来的SQL语句是这样的:LC_Sql := 'select * from tablename where'
    + lc_str + 'and code like '+ '''' +'001'+'*'+ '''';query1.sql.add(lc_Sql);
    ...
    ...lc_str 是界面操作得来的一个多项查询的条件,也就是比如下面的这个条件:
     name='asd' and info='INFO'
     
    现在的问题就是 当 lc_str是 name='asd'的时候,查询结果符合要求,也就是
    它既考虑了code like '001*' 也考虑了 name='asd' ;
    现在的问题是:lc_str 为两个以上的条件时,查询后的结果并没有执行
    code like '001*'的条件,而是从全部的记录中查旬。我用的是access97 + bde5+delphi5
    从表面上看这没有错误,可实际情况就是这样,不知你们这么看待?我刚刚受到启发,在 lc_Str 两边加了括号,结果正常了,不过我值得怀疑
    这种现象,或者说是为什么?最后,各位高人辛苦了,稍候结账,不管你回答正确与否,这不重要,重要的是
    我们要讨论问题,人人有赏!!:))
      

  12.   

    select * from tablename where name='asd' and info='INFO' and code in (select Code from tablename where code like '001*' ) 
      

  13.   

    是不是你的lc_str条件中有or? 这样就需要注意加括号了. 如果全是and应该不会有问题.
      

  14.   

    我把语句在ACCESS中试了一下,没有问题的,like的计算优先级比AND高。
    把生成的SQL放入MEMO,复制到ACCESS中执行看看。
      

  15.   

    LC_Sql := 
             'select * 
              from 
                ( select * from tablename where code like where 
                 code  like ''001%'') 
              tablename1
              Where ' + lc_str ; 
    query1.sql.add(lc_Sql); 如果不是你的lc_str子句组合有问题,这样一定行的,因为以前我这样用过!!
    希望你仔细一点,再试一试,一定会好用的。
    有一点耐心,你一定会成功的:)
      

  16.   

    再补充一下!!
    lc_str中要写明字段所隶属的表,是你的查询集所生成的表,如现在这个Tablename1
      

  17.   

    select * 
    from tablename 
    where code in (select code from tablename where  code like '%001%')
    and  name='asd' 
    and info='INFO'
     
    不知道你想到达什么效果,是不是如果没有符合code like '001*'它的条件时就显示全部,有则显示?
    如果是用视图就可以解决
    create view v_tablename
    as
    select *
    from tablename
    where name='asd' 
    and   info='INFO' select *
    from tablename t
    left join v_tablename v on v.id=t.id and  code like '%001%'
      

  18.   

    事实证明:select * from parts_list where (amount_one=2 or weight_one>8) and code in (select Code from parts_list
     where code like '002*' ) 和
    select * from parts_list where (amount_one=2 or weight_one>8) and  code like '002*' 两个的生成结果是一样的而
    select * from parts_list where amount_one=2 or weight_one>8 and  code like '002*' )  
    表达式能通过但结果不正确。前两种可以采纳,是正确的。to mysine(宝兰) :谢谢你,可是access不象sqlserver 它不支持 select * from (select * from ...) where 这样的语句,它提示 from子句错误。不过
    你的思路是正确的,非常感谢。
     kb(天堂游侠) 
    LXJ2001(lxj) 
    ling(ling) 你们都正确,是我犯糊涂了,呵呵,不过学了一招:
    select * from parts_list where (amount_one=2 or weight_one>8) and code in (select Code from parts_list
     where code like '002*' ) 
    这样写法很清楚,结构清晰,虽有些冗余,也是值得采纳,尤其是大型数据库的时候。