有两个表
user表   id              name               idCompany
000001             张三                 302000
000002             李四    301000company表idCompany  name
100000 总公司
201000 **分公司
202000 **分公司
301000 **营业部
302000 **营业部
company表的idCompany编号规则为
100000(总公司)
201000(**分公司)第1位代表为分公司,23位为分公司代码
202000(**分公司)
301000(营业部)第1位代表为营业部,第23位为上级分公司的代码
302000(营业部)我现在要查这些信息出来
用户名,所属分公司,所属营业部不知这条SQL语句要怎样写

解决方案 »

  1.   

    标题写错了,应该是一条简单的SQL语句.
      

  2.   

    select a.name,(select name from company where 
    substr(idcompany,1,3)='2'||substr(a.idcompany,2,2) ) as "所属分公司",b.name as "营业部" 
    from user a,company b where a.idcompany=b.idcompany;
      

  3.   

    name       name  所属分公司 营业部
    张三      深圳分公司      深圳分公司你给的语句查出的是这种结果
      

  4.   

    select a.name,b1.name as '所属分公司',b2.name as '营业部'
    from user a,company b1,company b2 where a.idcompany=b2.idcompany 
    and substr(a.idcompany,1,3)='3'||substr(b1.idcompany,2,2)
      

  5.   

    sorry 少了个条件:
    select a.name,b1.name as '所属分公司',b2.name as '营业部'
    from user a,company b1,company b2 where a.idcompany=b2.idcompany 
    and substr(a.idcompany,1,3)='3'||substr(b1.idcompany,2,2) and substr(b1,1,1)='2'
      

  6.   

    不知对否,如下
    select a.id,a.name,b1.name as '所属分公司',b2.name as '所属营业部'
    from user a,company b1,company b2 
    where a.idCompany=b2.idCompany and substr(a.idCompany,2,2)=substr(b1.idCompany,2,2);
      

  7.   

    substr(a.idcompany,1,3)='3'||substr(b1.idcompany,2,2)
    这个我不懂,那位能解释一下
      

  8.   

    select a.name,b1.name as '所属分公司',b2.name as '营业部'
    from user a,company b1,company b2 where a.idcompany=b2.idcompany 
    and substr(b2,1,1)='3'and substr(b1.idcompany,2,2)=substr(b2.idcompany,2,2) and substr(b1,1,1)='2'
      

  9.   

    create table USER1(
    ID varchar2(8),
    NAME varchar(20),
    IDCOMPANY varchar(8))INSERT INTO USER1 VALUES('000001','张三','302000')INSERT INTO USER1 VALUES('000002','李四','301000')create table company(
    IDCOMPANY VARCHAR(8),
    NAME VARCHAR(20))INSERT INTO company  VALUES('100000','总公司')INSERT INTO company  VALUES('201000','广东分公司')INSERT INTO company  VALUES('202000','深圳分公司')INSERT INTO company  VALUES('301000','xxx营业室')INSERT INTO company  VALUES('302000','yyy营业室')select * from user1select * from companyselect a.name,b1.name 所属分公司,b2.name  所属营业部
    FROM user1 a,company b1,company b2 
    where a.idcompany=b2.idcompany 
    and substr(b2.idcompany,1,1)='3'
    and substr(b1.idcompany,2,2)=substr(b2.idcompany,2,2) 
    and substr(b1.idcompany,1,1)='2'通过调试了,
    这个才是正确的
      

  10.   

    布衣你的可以,但你能不能帮我解释一下这句
    substr(a.idcompany,1,3)='3'||substr(b1.idcompany,2,2)
    这个我不懂