楼主不是要得到T1里p_name=370 在p_id=u_id的情况下,u_name也是370吗?

解决方案 »

  1.   

    select * from T1 where p_name=370
    union
    select * from T2 where u_name=370;--楼主想这样?
      

  2.   

    我的意思就是用一条SQL语句得到T1和T2里name字段都为370的数据!而且结果出来后不能数据重复!结果如下:
    p_id                 p_name
    1                      370
    2                      370
    3                      370
      

  3.   

    那用这个语句就可以了:
    select * from T1 where p_name=370
    union
    select * from T2 where u_name=370;--union 的作用是去掉重复的记录
      

  4.   

    如果是用union all 的话,就是联合所有,而不去掉重复的记录
      

  5.   

    不。我说错了,我要得到的结果是:在表T1里p_name为'370'和在表T2里u_name为'370'且从T2里查出来的数据u_id必须出现在T1的p_id里!大家明白了吗?我真的好着急呀!!!谢谢了!!
      

  6.   

    yjdn(无尽天空) :你的办法是把所有在T1和T2里name值为370的数据都查出来,可是在T2里u_name为370的不全出现在T1里呀!只是有一部分出现在T1里!
    我想得到的是T1.p_name='370' or (T2.u_name='370' and T2.u_id在T1.p_id里)。
    大家快帮帮我吧!
      

  7.   

    select * from T1 where p_name=370
    union
    select * from T2 where u_name=370 where u_id in (select p_id from T1 );--u_id在T1的p_id里,而不需要相等?
      

  8.   

    不行啊!数据库报错了!where u_name=370 where u_id in 这里怎么两个where啊?
    对,要求u_id在T1的p_id里出现!
      

  9.   

    呵呵,打错
    select * from T1 where p_name=370
    union
    select * from T2 where u_name=370 and u_id in (select p_id from T1 );
      

  10.   

    这样也不行啊。我倒是搞定了,我是这么写的:
    select p_id from T1 where p_name=370
    union
    select u_id from T2 where u_name=370 and u_id in (select p_id from T1 );
    但是只能取得p_id这一个字段的值啊,能不能把t1里的所有字段都取出来?如果用*号就报错。说是字段不匹配!!!
      

  11.   

    select * from T1 where p_name=370
    union
    select * from T2 where u_name=370 and u_id in (select p_id from T1 );
    --用这个是可以的,不会是你p_name和u_name的字段类型不同吧?
      

  12.   

    类型是相同的,但是字段名不一样啊。而且我想列出T1表中的所有字段,而T2和T1的字段是完全不同的。所以用select * from是不行的,系统报错了!!!
      

  13.   

    实在搞不懂你要达到什么目的:
    --执行
    select * from T1 where p_name=370
    union
    select * from T2 where u_name=370 and u_id in (select p_id from T1 );
    --结果:我没发现有什么错的地方       P_ID     P_NAME
    ---------- ----------
             1        370
             2        370
             3        370
      

  14.   

    select a.p_id,a.p_name,b.p_id,b.p_name from T1 a,T2 b where a.p_name=370 and b.p_name=370 group by a.p_id,a.p_name,b.p_id,b.p_name
      

  15.   

    试下新东东:
    select a.p_id,a.p_name,b.p_id,b.p_name from T1 a,T2 b where a.p_id=b.p_id and a.p_name=370 and b.p_name=370 group by a.p_id,a.p_name,b.p_id,b.p_name
      

  16.   

    select a.p_id,p_name=(case when a.p_name=370 then p_name else b.p_name end) from select * from t2 where u_name=370 a join (select * from t2 where u_name=370)  b on .p_id=b.p_id
      

  17.   

    select distinct id,name
    from (
    select p_id as id,p_name as name
    from t1
    where p_name='370'
          )
          unit
          (
    select u_id as id,u_name as name
    form t2
    where p_name='370'
          )