自关联


-- create table areas(
-- aid int primary key,
-- atitle varchar(20),
-- pid int
-- );
-- insert into areas
-- values ('130000', '河北省', NULL),
-- ('130100', '石家庄市', '130000'),
-- ('130400', '邯郸市', '130000'),
-- ('130600', '保定市', '130000'),
-- ('130700', '张家口市', '130000'),
-- ('130800', '承德市', '130000'),
-- ('410000', '河南省', NULL),
-- ('410100', '郑州市', '410000'),
-- ('410300', '洛阳市', '410000'),
-- ('410500', '安阳市', '410000'),
-- ('410700', '新乡市', '410000'),
-- ('410800', '焦作市', '410000');

-- (p 省 c 市 a 区 选择条件 就是找一个正确的 找一条错误的 看一下连接关系)

--  查询一共有多少个省

-- select count(*) from areas where pid is null;

-- 查询河南省的所有城市

-- select *from areas as p inner join areas as c on c.pid=p.aid where p.atitle='河南省';

-- insert into areas values
-- ('410101', '中原区', '410100'),
-- ('410102', '二七区', '410100'),
-- ('410103', '金水区', '410100');

-- 查询郑州市的所有区县

-- select * from areas as c inner join areas as a on a.pid=c.aid where c.atitle='郑州市';

-- 查询河南省的所有区县

-- select * from areas as p left join areas as c on c.pid=p.aid
-- left join areas as a on a.pid=c.aid where p.atitle='河南省'
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值