pjobdistrict(全国省市区)+----+--------+-------+------+
| id | name   | level | upid |
+----+--------+-------+------+
|  1 | 北京市 |     1 |    0 |
|  2 | 天津市 |     1 |    0 |
|  3 | 河北省 |     1 |    0 |
|  4 | 山西省 |     1 |    0 |
|  5 | 内蒙古 |     1 |    0 |
|  6 | 辽宁省 |     1 |    0 |
|  7 | 吉林省 |     1 |    0 |
|  8 | 黑龙江 |     1 |    0 |
|  9 | 上海市 |     1 |    0 |
| 10 | 江苏省 |     1 |    0 |
| 11 | 浙江省 |     1 |    0 |
| 12 | 安徽省 |     1 |    0 |
| 13 | 福建省 |     1 |    0 |
| 14 | 江西省 |     1 |    0 |
| 15 | 山东省 |     1 |    0 |
| 16 | 河南省 |     1 |    0 |
| 17 | 湖北省 |     1 |    0 |
| 18 | 湖南省 |     1 |    0 |
| 19 | 广东省 |     1 |    0 |
| 20 | 广西省 |     1 |    0 |
| 21 | 海南省 |     1 |    0 |
| 22 | 重庆市 |     1 |    0 |
| 23 | 四川省 |     1 |    0 |
| 24 | 贵州省 |     1 |    0 |
| 25 | 云南省 |     1 |    0 |
| 26 | 西藏   |     1 |    0 |
| 27 | 陕西省 |     1 |    0 |
| 28 | 甘肃省 |     1 |    0 |
| 29 | 青海省 |     1 |    0 |
| 30 | 宁夏   |     1 |    0 |
| 31 | 新疆   |     1 |    0 |
| 32 | 台灣   |     1 |    0 |
| 33 | 香港   |     1 |    0 |
| 34 | 澳门   |     1 |    0 |
| 35 | 海外   |     1 |    0 |
| 36 | 其他   |     1 |    0 |
+----+--------+-------+------+upid=23(四川省)+-----+--------------------+-------+------+
| id  | name               | level | upid |
+-----+--------------------+-------+------+
| 385 | 成都市             |     2 |   23 |
| 386 | 自贡市             |     2 |   23 |
| 387 | 攀枝花市           |     2 |   23 |
| 388 | 泸州市             |     2 |   23 |
| 389 | 德阳市             |     2 |   23 |
| 390 | 绵阳市             |     2 |   23 |
| 391 | 广元市             |     2 |   23 |
| 392 | 遂宁市             |     2 |   23 |
| 393 | 内江市             |     2 |   23 |
| 394 | 乐山市             |     2 |   23 |
| 395 | 南充市             |     2 |   23 |
| 396 | 眉山市             |     2 |   23 |
| 397 | 宜宾市             |     2 |   23 |
| 398 | 广安市             |     2 |   23 |
| 399 | 达州市             |     2 |   23 |
| 400 | 雅安市             |     2 |   23 |
| 401 | 巴中市             |     2 |   23 |
| 402 | 资阳市             |     2 |   23 |
| 403 | 阿坝藏族羌族自治州 |     2 |   23 |
| 404 | 甘孜藏族自治州     |     2 |   23 |
| 405 | 凉山彝族自治州     |     2 |   23 |
+-----+--------------------+-------+------++------+----------+-------+------+
| id   | name     | level | upid |
+------+----------+-------+------+
| 4209 | 双流县   |     3 |  385 |
| 4210 | 大邑县   |     3 |  385 |
| 4211 | 崇州市   |     3 |  385 |
| 4212 | 彭州市   |     3 |  385 |
| 4213 | 成华区   |     3 |  385 |
| 4214 | 新津县   |     3 |  385 |
| 4215 | 新都区   |     3 |  385 |
| 4216 | 武侯区   |     3 |  385 |
| 4217 | 温江区   |     3 |  385 |
| 4218 | 蒲江县   |     3 |  385 |
| 4219 | 邛崃市   |     3 |  385 |
| 4220 | 郫县     |     3 |  385 |
| 4221 | 都江堰市 |     3 |  385 |
| 4222 | 金堂县   |     3 |  385 |
4223 | 金牛区   |     3 |  385 |
| 4224 | 锦江区   |     3 |  385 |
| 4225 | 青白江区 |     3 |  385 |
| 4226 | 青羊区   |     3 |  385 |
| 4227 | 龙泉驿区 |     3 |  385 |
+------+----------+-------+------+
上面是一个大概的内容,我的需求是,
假如我有一个表叫 User 里面有两个字段分别叫 pjobdistrict_id和user_id;
pjobdistrict_id值4223,user_id值1     4223这个值对应的是金牛区(属于四川省),user_id值1对应a用户。
用户a 选择 (四川省)提交到后台的值为 23  我要的结果是通过23这个值,写一条SQL 将User表包涵的值4223(金牛区属于四川省)返回出去!

解决方案 »

  1.   

    lz你只传一个23(省份的Id)要查询User 吗
    还是不大理解你的需求
      

  2.   

    将User表包涵的值4223(金牛区属于四川省)返回出去!
    这是神马意思
      

  3.   

    通过省份的ID   查询到user 中包涵的市、县的值比如 金牛区(4223) 就属于四川省 ,那么结果就应该返回4223假如User表
    user_id    pjobdistrict_id
    1                4223
    1                1814(外滩街道 它属于上海)那么结果应该只返回4223而1814就不应该返回    
      

  4.   

    select pjobdistrict_id from user
    where  upid in (
            select id from pjobdistrict
             where  upid=23
           );
      

  5.   

    猜测了下LZ的意思,看看这个可以不:select a.pjobdistrict_id from user a where a=1 and exists(
      select 1 from pjobdistrict b where b.pjobdistrict_id=a.pjobdistrict_id and exists(
         select 1 from pjobdistrict c where c.upid=23 and c.pjobdistrict_id = b.upid
      )
    )
      

  6.   

    不是 应该是两表join
    select pjobdistrict_id from user,pjobdistrict
    where pjobdistrict_id=id and
      upid in (
      select id from pjobdistrict
      where upid=23
      );
      

  7.   

    说简单点就是,user表中的pjobdistrict_id 只保存了县级市的ID,当前用户通过省份的ID查出县级市对应的ID,在不能理解,我要无语啦!
      

  8.   


    select a.pjobdistrict_id
    from [user] a join (select id from pjobdistrict where upid in (select id from pjobdistrict where upid = 23)) b on a.pjobdistrict_id = b.id
    where user_id = 1--不知道对不对!
      

  9.   

    select a.pjobdistrict_id from user a where a=1 and exists
    (select p.id from pjobdistrict p 
    start with p.upid = 23 connect by prior p.id = p.upid)
      

  10.   


    try:select a.pjobdistrict_id from user a where a=1 and exists( select 1 from pjobdistrict b where b.pjobdistrict_id=a.pjobdistrict_id and exists( select 1 from pjobdistrict c where c.upid=23 and c.pjobdistrict_id = b.upid ) ) 
      

  11.   

    做三次关联即可select d.name --这个就是省份
    from ueser a 
    left join pjobdistrict b on a.pjobdistrict_id = b.upid  and b.level=3
    left join pjobdistrict c on b.upid = c.id   and c.level=2
    left join pjobdistrict d on c.upid = d.id  and d.level=1where user_id=1
      

  12.   

    select u.user_id,x.id from user as u,省份表 as s1,市级表 as s2,区县表 as x
     where u.pjobdistrict_id=s1.id and s1.id=s2.upid and s2.id=x.upid楼主可以试试,不行可以再告诉我
      

  13.   


    select u.user_id,x.id from user as u,省份表 as s1,市级表 as s2,区县表 as x
     where u.user_id=1 and u.pjobdistrict_id=s1.id and s1.id=s2.upid and s2.id=x.upid 修改一下
      

  14.   


    select name from user,pjobdistrict 
    where user_id=1
    and   pjobdistrict_id=id 
    and   upid  in
               (select id from pjobdistrict where upid=23)
    ;
      

  15.   

    select u.user_id,x.id from user as u,省份表 as s1,市级表 as s2,区县表 as x
     where u.user_id=1 and u.pjobdistrict_id=s1.id and s1.id=s2.upid and s2.id=x.upid 刚发现有个符号写错了,修改如上 
      

  16.   


    select name 
    from user,pjobdistrict  
    where user_id=1
    and pjobdistrict_id=id  
    and upid in
               (select id from pjobdistrict where upid=23)
    ;
      

  17.   

    你是不是想说,当用户前台选择了省之后,后台接收到省的ID,根据这个ID,把所有县/县级市/区的id select出来??SELECT b.id
      FROM pjobdistrict a, -- 市/地级市
           pjobdistrict b -- 县/县级市/区
     WHERE a.id = b.upid -- 县/县级市/区的上级单位与市/地级市关连
       AND a.upid = 23 -- 市的上级是你传进来的值,变量
       AND a.level = 2 -- 固定值,即只选取所有市/地级市级别
       AND b.level = 3 -- 固定值,即只选取所有县/县级市/区级别
      

  18.   

    oracle 层次查询...一条搞定
      

  19.   

    select connect_by_isleaf,sys_connect_by_path(child,'/') 
    pathfrom hierstart with parent is null connect by prior child = parent;
      

  20.   

    你的意思是传节点23进去,返回23的所有子节点吧?
    你得说明是在SQL Server还是Oracle或其他数据库,在SQL里递归一下就搞定.
      

  21.   


    select a.pjobdistrict_id
    from [user] a join (select id from pjobdistrict where upid in (select id from pjobdistrict where upid = 23)) b on a.pjobdistrict_id = b.id
    where user_id = 1
      

  22.   

    下次楼主还是去SQL版问吧,哪里高手都积极的很