表:test
id  mcid  sjid 
1     2    0
2     3    1
3     4    2问题:
知道id为3,sjid = id 得到id为2的mcid?不用子查询:
select mcid from test where id = (select sjid from test where id = 3);
该怎么写?

解决方案 »

  1.   

    select mcid from test where id in (select sjid from test where id = 3); 
      

  2.   

    select t2.mcid from test t1 join test t2 on t1.id=t2.sjid where t1.id=3
    大概写的,没实际跑过( - __ - )y一~ 
      

  3.   

    知道id为3,sjid = id 得到id为2的mcid? 
    楼主没有发现你这句有歧义么你要取ID为2 还是MCID为2的数据?
    如果是后者4楼可以  如果是前者 无法实现。
    2楼使用了子查询 好像不符合楼主要求了。
      

  4.   

    4楼 查出来的是下级的mcid 我要的是上级的
      

  5.   

    通过id=3 得到这条记录的sjid
    通过外键,得到id = sjid 记录的mcid