我写了一个语句:
select d_name as '区县',乡镇,普查区,普查小区 from dress,
(select d_name as '乡镇',普查区,普查小区,d_pid as pid from dress,
(select num as '普查小区',d_name as '普查区',d_pid as pid
from pcxq,dress
where pcxq.code=dress.d_code) as pcq
where dress.d_id=pcq.pid) as xz
where dress.d_id=xz.pid
感觉可能复杂了,求高手指点。
1、用连接要结构清晰些,不需要那么多子查询。
2、你的d_code存储得很有规范,不一定需要用d_pid关联。
SELECT 区县=t4.d_name,乡镇=t3.d_name,普查区=t2_dname,普查小区=t1.num FROM pcxq t1 LEFT JOIN dress t2 ON t2.d_code=t1.code LEFT JOIN dress t3 ON t3.d_code=LEFT(t1.code,9) LEFT JOIN dress t4 ON t4.d_code=LEFT(t1.code,6)
非常感谢您的指点。谢谢。
@duguao: 共同进步~
没大看明白
你把dress分成3个表被:区,处,委员会,然后连接3个表,结构会清晰些
谢谢你的建议。