select distinct a.id,a.name from tablea a join tableb b on a.id=b.id
where (brief like '%friday%' or brief like '%9%') and state= 'U' and status= 'UN'

解决方案 »

  1.   


    select id,name from tablea where (brief like '%friday%' or brief like '%9%') and (state='U' or state='UN')
    union
    select id,name from tableb where status='U' or status='UN'
      

  2.   


    create table #tablea (id int,name varchar(10),brief varchar(10),state varchar(10))
    insert into #tablea
    select 1,'adf','friday','U'
    union all
    select 2,'jkl','9,16','U'
    union all
    select 3,'gde','9,22','N'create table #tableb(id int,name varchar(10),status varchar(10))
    insert into #tableb 
    select 1,'adf','U'
    union all
    select 2,'jkl','UN'
    select a.id,a.name from #tablea a left join #tableb b on a.id=b.id 
    where (brief like '%friday%' or brief like '%9%') and state='U'and status='U'--结果
    id          name       
    ----------- ---------- 
    1           adf(所影响的行数为 1 行)
      

  3.   

    select a.* , b.* from tablea a,tableb 
    where (a.brief like '%friday%' or a.brief like '%9%') and a.state= 'U' and b.status= 'U '
      

  4.   

    对不起,问题没说清楚!用union只能针对有限的数据,下面的数据只是枚举
    谢谢你们
    有两张表:tablea,tableb   其中tablea   表中有字段  
    id   name     brief       state   ; 
    1    adf     thursday       U 
    2     jkl       16      U 
    3     gde       22       N 
    4      hhh      friday     U
    tableb表中有字段 
    id     name     status 
    1         adf         U 
    2         jkl         UN 
    3         gde         UN
    现在要查询两张表中brief   like   'friday或9 '   且   state= 'U '   或者status= 'UN '的id和name;
    希望得到的结果是
    4 hhh (属于a表)
    2 jkl (b表)
    3 gde (b表)
      

  5.   

    to:LZ
    這樣看來,我在2樓的回復是正確的
      

  6.   

    是呀,但你用的是union,如果数据很多怎么办?
      

  7.   

    飞天小虫下面的方法正确了,我自己试了试,你的union也正确,但数据多了会影响执行速度!呵呵,同样谢谢你!
      

  8.   

    飞天小虫下面的方法正确了
    --------------------------------
    不用union,未必正確,樓主你試下,如果數據是下面這樣
    id   name     brief       state   ; 
    1 adf       friday       U 
    2     jkl       9,16         U 
    tableb表中有字段 
    id     name     status 
    1         adf         U 
    2         jkl         UN 
    3         gde         UN 
      

  9.   

    也是哦~我倒是没有考虑到b表比a表数据多的情况,因为我的b表都是从a表弄进去的 那应该怎么办呢?
      

  10.   

    除了union,我沒想到好的辦法,如果不用union,可是可以,感覺太麻煩了,性能未必就好.
    期待高人入內
      

  11.   

    select * from savea,savea1
    where brief like '%friday%'or brief like '%9%' and state='u'or state='vu' and savea.id=savea1.id