数据库——连接、多表查询
--创建学生表
create table students ( id int unsigned not null auto_increment primary key, name varchar(20) default '', age tinyint unsigned default 0, high decimal(5,2), gender enum('男', '女', '中性', '保密') default '保密', cls_id int unsigned default 0, is_delete bit default 0 );
--创建班级表
create table classes( id int unsigned auto_increment primary key not null, name varchar(20) not null );
--往students表里插入数据
insert into students values (0,'小明',18,180.00,2,1,0), (0,'小月月',19,180.00,2,2,0), (0,'彭于晏',28,185.00,1,1,0), (0,'刘德华',58,175.00,1,2,0), (0,'黄蓉',108,160.00,2,1,0), (0,'凤姐',44,150.00,4,2,1), (0,'王祖贤',52,170.00,2,1,1), (0,'周杰伦儿',34,null,1,1,0), (0,'程坤',44,181.00,1,2,0), (0,'和珅',55,166.00,2,2,0), (0,'刘亦菲',29,162.00,3,3,0), (0,'金星',45,180.00,2,4,0), (0,'静香',18,170.00,1,4,0), (0,'郭静',22,167.00,2,5,0), (0,'周杰',33,178.00,1,1,0), (0,'钱小豪',56,178.00,1,1,0), (0,'谢霆锋',38,175.00,1,1,0);
--向classes表里插入数据
insert into classes values (0, '云唯_01期'),(0, '云唯_02期');
连接查询(内关联)
-- inner join ... on
-- 两个表连接查询
select * from students inner join classes
-- 查询能够对应班级的学生以及班级信息
select * from students inner join classes on students.cls_id=classes.id;
-- 按照要求显示姓名,班级
select students.*, classes.name from students inner join classes on students.cls_id=classes.id;
-- 给数据表起名字
select s.name, c.name from students as s inner join classes as c on s.cls_id=c.id;
-- 查询 有能够对应班级的学生以及班级信息,显示学生的所有信息,只显示班级名称
select students.*, classes.name from students inner join classes on students.cls_id=classes.id;
-- 在以上查询中将班级姓名显示在第一列
select classes.name,students.* from students inner join classes on students.cls_id=classes.id;
-- 查询有能够对应班级的学生以及班级信息,按照班级进行排序
select classes.id, students.* from students inner join classes on students.cls_id=classes.id order by classes.id;
-- 当是同一个班级的时候,按照学生的id从小到大
select classes.id, students.* from students inner join classes on students.cls_id=classes.id order by classes.id, students.id;
连接查询(左关联,右关联)
--左关联,以左边表为基准,条件对应不上的显示null
select * from students left join classes on students.cls_id=classes.id;
--右关联,以后边为基准,条件对应不上的显示null
select * from classes left join students on students.cls_id=classes.id;
自关联
create table areas( aid int primary key auto_increment, name varchar(20), pid int );
use test; --省份 insert into areas(aid,name,pid) values(1,'北京市',null); insert into areas(aid,name,pid) values(0,'天津市',null); insert into areas(aid,name,pid) values(0,'河北省',null); insert into areas(aid,name,pid) values(0,'山西省',null); --地级市 insert into areas(aid,name,pid) values(0,'海淀区',1); insert into areas(aid,name,pid) values(0,'滨海区',2); insert into areas(aid,name,pid) values(0,'沧州市',3); insert into areas(aid,name,pid) values(0,'大同市',4); insert into areas(aid,name,pid) values(0,'朝阳区',1); insert into areas(aid,name,pid) values(0,'武清区',2); insert into areas(aid,name,pid) values(0,'石家庄',3); insert into areas(aid,name,pid) values(0,'太原市',4); --县级市 insert into areas(aid,name,pid) values(0,'西二旗',5); insert into areas(aid,name,pid) values(0,'大港',6); insert into areas(aid,name,pid) values(0,'任丘市',7); insert into areas(aid,name,pid) values(0,'清徐',8); insert into areas(aid,name,pid) values(0,'中关村',5); insert into areas(aid,name,pid) values(0,'汉沽',6); insert into areas(aid,name,pid) values(0,'河间市',7); insert into areas(aid,name,pid) values(0,'阳曲',8);
-- 查询出河北省所有市
select * from areas as province inner join areas as city on province.aid=city.pid having name='河北省';
select province.name,city.name from areas as province inner join areas as city on province.aid=city.pid having province.name='河北省';
-- 子查询
-- 标量子查询
-- 查询出北京市所有区的信息
select * from areas where pid=(select aid from areas where name='北京市');
select * from areas where pid in (select aid from areas where name='北京市');
作者:无荨
-------------------------------------------
个性签名:学IT,就要做到‘活到老学到老’!
如果觉得这篇文章对你有小小的帮助的话,别忘记点个“推荐”哦!