本篇主要介绍使用SQL查询数据库的操作,包括条件查询、排序、聚合函数、分组、分页、连接查询、自关联、子查询等命令操作。
首先我们先创建一个数据库、数据表、插入字段:
--------这部分在上篇以及介绍了----------------- -- 创建数据库 create database blogs_test charset=utf8; -- 使用数据库 use blogs_test; -- students表 create table students( id int unsigned primary key auto_increment not null, name varchar(20) default \'\', age tinyint unsigned default 0, height decimal(5,2), gender enum(\'男\',\'女\',\'中性\',\'保密\') default \'保密\', cls_id int unsigned default 0, is_delete bit default 0 ); -- classes表 create table classes ( id int unsigned auto_increment primary key not null, name varchar(30) not null ); ---------------插入记录---------------------- -- 向students表中插入数据 insert into students values (0,\'小明\',18,180.00,2,1,0), (0,\'小月月\',18,180.00,2,2,1), (0,\'彭于晏\',29,185.00,1,1,0), (0,\'刘德华\',59,175.00,1,2,1), (0,\'黄蓉\',38,160.00,2,1,0), (0,\'凤姐\',28,150.00,4,2,1), (0,\'王祖贤\',18,172.00,2,1,1), (0,\'周杰伦\',36,NULL,1,1,0), (0,\'程坤\',27,181.00,1,2,0), (0,\'刘亦菲\',25,166.00,2,2,0), (0,\'金星\',33,162.00,3,3,1), (0,\'静香\',12,180.00,2,4,0), (0,\'郭靖\',12,170.00,1,4,0), (0,\'周杰\',34,176.00,2,5,0); -- 向classes表中插入数据 insert into classes values (0, "python_01期"), (0, "python_02期"); ---得到的students表结构如下: +----+-----------+------+--------+--------+--------+-----------+ | id | name | age | height | gender | cls_id | is_delete | +----+-----------+------+--------+--------+--------+-----------+ | 1 | 小明 | 18 | 180.00 | 女 | 1 | | | 2 | 小月月 | 18 | 180.00 | 女 | 2 | | | 3 | 彭于晏 | 29 | 185.00 | 男 | 1 | | | 4 | 刘德华 | 59 | 175.00 | 男 | 2 | | | 5 | 黄蓉 | 38 | 160.00 | 女 | 1 | | | 6 | 凤姐 | 28 | 150.00 | 保密 | 2 | | | 7 | 王祖贤 | 18 | 172.00 | 女 | 1 | | | 8 | 周杰伦 | 36 | NULL | 男 | 1 | | | 9 | 程坤 | 27 | 181.00 | 男 | 2 | | | 10 | 刘亦菲 | 25 | 166.00 | 女 | 2 | | | 11 | 金星 | 33 | 162.00 | 中性 | 3 | | | 12 | 静香 | 12 | 180.00 | 女 | 4 | | | 13 | 郭靖 | 12 | 170.00 | 男 | 4 | | | 14 | 周杰 | 34 | 176.00 | 女 | 5 | | +----+-----------+------+--------+--------+--------+-----------+ 14 rows in set (0.00 sec) --得到的classes表结构如下: +----+--------------+ | id | name | +----+--------------+ | 1 | python_01期 | | 2 | python_02期 | +----+--------------+ 2 rows in set (0.00 sec)
|
-- 语法结构 ............... order by 字段 desc |
一、条件查询
-- 消除重复行 -- distinct 字段 select district gender from students; -- 条件查询 -- 1、比较运算符 -- select .... from 表名 where ..... -- > -- 查询大于18岁的信息 select * from students where age>18; select id,name,gender from students where age>18; -- < -- 查询小于18岁的信息 select * from students where age<18; -- >= -- <= -- 查询小于或等于18岁的信息 -- = --查询年龄为18岁的所有学生的名字 select * from students where age=18; -- != 或者 <> -- 2、逻辑运算符 -- and -- 18到28岁之间所有学生的信息 select * from students where age>18 and age<28; -- 失败select * from students where age>18 and <28; --18岁以上的女性 select * from students where age>18 and gender="女"; select * from students where age>18 and gender=2; -- or --18岁以上或者身高查过180(包含)以上 select * from students where age>18 and height>=180; -- not -- 不在 18岁以上的女性 这个范围内的信息 select * from students where not age>18 and gender=2; select * from students where not (age>18 and gender=2); -- 年龄不是小于或者等于18 并且是女性 select * from students where (not age<=18) and gender=2; --3、模糊查询 -- like -- % 替换1个或者多个 -- _ 替换1个 -- 查询姓名中 以 "小" 开始的名字 select name from students where name="小"; select name from students where name like "小%"; -- 查询姓名中 有"小" 所有的名字 select name from students where name like "%小%"; -- 查询有2个字的名字 select name from students where name like "__"; -- 查询有3个字的名字 select name from students where name like "___"; -- 查询至少有2个字的名字 select name from students where name like "__%"; -- rlike 正则 -- 查询以 周开始的姓名 select name from students where name rlike "^周.*"; -- 查询已 周开始、伦结尾的姓名 select name from students where name rlike "^周.*伦$"; --4、范围查询 -- in(1, 3,8 )表示在一个非连续的范围内 -- 查询 年龄为18、34的名字 select name,age from students where age=18 or age=34; select name,age from students where age=18 or age=34 or age=12; select name,age from students where age in (12, 18, 34); -- not in 不非连续的范围之内 -- 年龄不是 18、34岁之间的信息 select name,age from students where age not in (12, 18, 34); -- between ... and ...表示在一个连续的范围内 -- 查询 年龄在18到34之间的信息 select name,age from students where age between 18 and 34; -- not between ... and ...表示不在一个连续的范围内 -- 查询 年龄不在在18到34之间的的信息 select * from students where age not between 18 and 34; select * from students where not age between 18 and 34; -- 失败的select * from students where age not (between 18 and 34); --5、空判断 -- 判空is null -- 查询身高为空的信息 select * from students where height is null; select * from students where height is NULL; select * from students where height is Null; -- 判非空is not null select * from students where height is not null;
条件查询 -- 主要用 where +条件 进行查询;
二、排序
-- --------------- 排序 -------------------------------- -- order by 字段 -- asc从小到大排列,即升序 -- desc从大到小排序,即降序 -- 查询年龄在18到34岁之间的男性,按照年龄从小到到排序 select * from students where (age between 18 and 34) and gender=1; select * from students where (age between 18 and 34) and gender=1 order by age; select * from students where (age between 18 and 34) and gender=1 order by age asc; -- 查询年龄在18到34岁之间的女性,身高从高到矮排序 select * from students where (age between 18 and 34) and gender=2 order by height desc; -- order by 多个字段 -- 查询年龄在18到34岁之间的女性,身高从高到矮排序, 如果身高相同的情况下按照年龄从小到大排序 select * from students where (age between 18 and 34) and gender=2 order by height desc, age asc; -- 查询年龄在18到34岁之间的女性,身高从高到矮排序, 如果身高相同的情况下按照年龄从小到大排序, -- 如果年龄也相同那么按照id动大到小排序 select * from students where (age between 18 and 34) and gender=2 order by height desc, age asc, id desc; -- 按照年龄从小到大、身高从高到矮的排序 select * from students order by age asc, height desc;
排序 -- order by + 以什么排序;
三、聚合函数
-- 聚合函数 -- 总数 -- count -- 查询男性有多少人,女性有多少人 select * from students where gender=1; select count(*) from students where gender=1; select count(*) as 男性人数 from students where gender=1; select count(*) as 女性人数 from students where gender=2; -- 最大值 -- max -- 查询最大的年龄 select age from students; select max(age) from students; -- 查询女性的最高 身高 select max(height) from students where gender=2; -- 最小值 -- min -- 求和 -- sum -- 计算所有人的年龄总和 select sum(age) from students; -- 平均值 -- avg -- 计算平均年龄 select avg(age) from students; -- 计算平均年龄 sum(age)/count(*) select sum(age)/count(*) from students; -- 四舍五入 round(123.23 , 1) 保留1位小数 -- 计算所有人的平均年龄,保留2位小数 select round(sum(age)/count(*), 2) from students; select round(sum(age)/count(*), 3) from students; -- 计算男性的平均身高 保留2位小数 select round(avg(height), 2) from students where gender=1; -- select round(avg(height), 2) from students where gender=1;
聚合函数 -- count(求个数) 、max(最大值) 、min(最小值) 、sum(求和)、avg(求平均值)、round(取小数点后几位、四舍五入的方式)。聚合函数通常配合下面的分组进行使用。
四、分组
-- 分组 -- group by -- 按照性别分组, 查询所有的性别 --以下两种方式会报错:由于name和*的记录数和分组不同步 select name from students group by gender; select * from students group by gender; -- 正确 select gender from students group by gender; -- 失败select * from students group by gender; -- 计算每种性别中的人数 select gender,count(*) from students group by gender; -- 计算男性的人数 select gender,count(*) from students where gender=1 group by gender; -- group_concat(...) -- 查询同种性别中的姓名 select gender, group_concat(name) from students where gender=1 group by gender; select gender, group_concat(name, age, id) from students where gender=1 group by gender; select gender, group_concat(name, "_", age, " ", id) from students where gender=1 group by gender; -- having -- 查询平均年龄超过30岁的性别,以及姓名 having avg(age) > 30 select gender, group_concat(name), avg(age) from students group by gender having avg(age)>30; -- 查询每种性别中的人数多于2个的信息 select gender, group_concat(name) from students group by gender having count(*)>2;
分组 -- group by + 以什么分组;group_concat()--以分组查看组内某些信息;having -- 筛选某种组显示;
五、分页
-- 分页 -- limit start, count -- 限制查询出来的数据格式 select * from students where gender=1 limit 2; -- 查询前5个数据 select * from students limit 0,5; -- 查询id6-10(包含)的书序 select * from students limit 5,5; -- 每页显示2个,第1个页面 select * from students limit 0,2; -- 每页显示2个,第2个页面 select * from students limit 2,2; -- 每页显示2个,第3个页面 select * from students limit 4,2; -- 每页显示2个,第4个页面 select * from students limit 6,2; -- -----> limit (第N页-1)*每个的个数, 每页的个数; -- 每页显示2个,显示第6页的信息, 按照年龄从小到大排序 -- 失败select * from students limit 2*(6-1),2; -- 失败select * from students limit 10,2 order by age asc; select * from students order by age asc limit 10,2; select * from students where gender=2 order by age desc limit 0,2;
分页 -- limit start count 从第几条数据开始显示,显示几个记录;
六、连接查询
-- 链接查询 -- inner join ... on -- select * from 表A inner join 表B; 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 students.name 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 s.*,c.name from students as s inner join classes as c on s.cls_id=c.id; -- 在以上的查询中,将班级姓名显示在第1列 select c.name,s.* from students as s inner join classes as c on s.cls_id=c.id; -- 查询 有能够对应班级的学生以及班级信息, 按照班级进行排序 -- select c.xxx s.xxx from student as s inner join clssses as c on .... order by ....; select c.name, s.* from students as s inner join classes as c on s.cls_id=c.id order by c.name; -- 当时同一个班级的时候, 按照学生的id进行从小到大排序 select c.name, s.* from students as s inner join classes as c on s.cls_id=c.id order by c.name,s.id; -- left join -- 查询每位学生对应的班级信息 select * from students as s left join classes as c on s.cls_id=c.id; -- 查询没有对应班级信息的学生 -- select ... from xxx as s left join xxx as c on..... where ..... -- select ... from xxx as s left join xxx as c on..... having ..... select s.*, c.name from students as s left join classes as c on s.cls_id=c.id where c.name is null; select s.*, c.name from students as s left join classes as c on s.cls_id=c.id having c.name is null; -- right join -- 将数据表名字互换位置,用left join完成
通常根据数据表B的某些条件显示数据表A的数据;inner/left/right join ... on ..;
七、自关联
INSERT INTO `areas` VALUES (\'110000\', \'北京市\', NULL); INSERT INTO `areas` VALUES (\'110100\', \'北京市\', \'110000\'); INSERT INTO `areas` VALUES (\'110101\', \'东城区\', \'110100\'); INSERT INTO `areas` VALUES (\'110102\', \'西城区\', \'110100\'); INSERT INTO `areas` VALUES (\'110103\', \'朝阳区\', \'110100\'); INSERT INTO `areas` VALUES (\'110104\', \'丰台区\', \'110100\'); INSERT INTO `areas` VALUES (\'110105\', \'石景山区\', \'110100\'); INSERT INTO `areas` VALUES (\'110106\', \'海淀区\', \'110100\'); INSERT INTO `areas` VALUES (\'110107\', \'门头沟区\', \'110100\'); INSERT INTO `areas` VALUES (\'110108\', \'房山区\', \'110100\'); INSERT INTO `areas` VALUES (\'110109\', \'通州区\', \'110100\'); INSERT INTO `areas` VALUES (\'110110\', \'顺义区\', \'110100\'); INSERT INTO `areas` VALUES (\'110111\', \'昌平区\', \'110100\'); INSERT INTO `areas` VALUES (\'110112\', \'大兴区\', \'110100\'); INSERT INTO `areas` VALUES (\'110113\', \'怀柔区\', \'110100\'); INSERT INTO `areas` VALUES (\'110114\', \'平谷区\', \'110100\'); INSERT INTO `areas` VALUES (\'110115\', \'密云县\', \'110100\'); INSERT INTO `areas` VALUES (\'110116\', \'延庆县\', \'110100\'); INSERT INTO `areas` VALUES (\'120000\', \'天津市\', NULL); INSERT INTO `areas` VALUES (\'120100\', \'天津市\', \'120000\'); INSERT INTO `areas` VALUES (\'120101\', \'和平区\', \'120100\'); INSERT INTO `areas` VALUES (\'120102\', \'河东区\', \'120100\'); INSERT INTO `areas` VALUES (\'120103\', \'河西区\', \'120100\'); INSERT INTO `areas` VALUES (\'120104\', \'南开区\', \'120100\'); INSERT INTO `areas` VALUES (\'120105\', \'河北区\', \'120100\'); INSERT INTO `areas` VALUES (\'120106\', \'红桥区\', \'120100\'); INSERT INTO `areas` VALUES (\'120107\', \'滨海新区\', \'120100\'); INSERT INTO `areas` VALUES (\'120108\', \'东丽区\', \'120100\'); INSERT INTO `areas` VALUES (\'120109\', \'西青区\', \'120100\'); INSERT INTO `areas` VALUES (\'120110\', \'津南区\', \'120100\'); INSERT INTO `areas` VALUES (\'120111\', \'北辰区\', \'120100\'); INSERT INTO `areas` VALUES (\'120112\', \'武清区\', \'120100\'); INSERT INTO `areas` VALUES (\'120113\', \'宝坻区\', \'120100\'); INSERT INTO `areas` VALUES (\'120114\', \'宁河县\', \'120100\'); INSERT INTO `areas` VALUES (\'120115\', \'静海县\', \'120100\'); INSERT INTO `areas` VALUES (\'120116\', \'蓟县\', \'120100\'); INSERT INTO `areas` VALUES (\'130000\', \'河北省\', NULL); INSERT INTO `areas` VALUES (\'130100\', \'石家庄市\', \'130000\'); INSERT INTO `areas` VALUES (\'130102\', \'长安区\', \'130100\'); INSERT INTO `areas` VALUES (\'130103\', \'桥东区\', \'130100\'); INSERT INTO `areas` VALUES (\'130104\', \'桥西区\', \'130100\'); INSERT INTO `areas` VALUES (\'130105\', \'新华区\', \'130100\'); INSERT INTO `areas` VALUES (\'130107\', \'井陉矿区\', \'130100\'); INSERT INTO `areas` VALUES (\'130108\', \'裕华区\', \'130100\'); INSERT INTO `areas` VALUES (\'130121\', \'井陉县\', \'130100\'); INSERT INTO `areas` VALUES (\'130123\', \'正定县\', \'130100\'); INSERT INTO `areas` VALUES (\'130124\', \'栾城县\', \'130100\'); INSERT INTO `areas` VALUES (\'130125\', \'行唐县\', \'130100\'); INSERT INTO `areas` VALUES (\'130126\', \'灵寿县\', \'130100\'); INSERT INTO `areas` VALUES (\'130127\', \'高邑县\', \'130100\'); INSERT INTO `areas` VALUES (\'130128\', \'深泽县\', \'130100\'); INSERT INTO `areas` VALUES (\'130129\', \'赞皇县\', \'130100\'); INSERT INTO `areas` VALUES (\'130130\', \'无极县\', \'130100\'); INSERT INTO `areas` VALUES (\'130131\', \'平山县\', \'130100\'); INSERT INTO `areas` VALUES (\'130132\', \'元氏县\', \'130100\'); INSERT INTO `areas` VALUES (\'130133\', \'赵县\', \'130100\'); INSERT INTO `areas` VALUES (\'130181\', \'辛集市\', \'130100\'); INSERT INTO `areas` VALUES (\'130182\', \'藁城市\', \'130100\'); INSERT INTO `areas` VALUES (\'130183\', \'晋州市\', \'130100\'); INSERT INTO `areas` VALUES (\'130184\', \'新乐市\', \'130100\'); INSERT INTO `areas` VALUES (\'130185\', \'鹿泉市\', \'130100\'); INSERT INTO `areas` VALUES (\'130200\', \'唐山市\', \'130000\'); INSERT INTO `areas` VALUES (\'130202\', \'路南区\', \'130200\'); INSERT INTO `areas` VALUES (\'130203\', \'路北区\', \'130200\'); INSERT INTO `areas` VALUES (\'130204\', \'古冶区\', \'130200\'); INSERT INTO `areas` VALUES (\'130205\', \'开平区\', \'130200\'); INSERT INTO `areas` VALUES (\'130207\', \'丰南区\', \'130200\'); INSERT INTO `areas` VALUES (\'130208\', \'丰润区\', \'130200\'); INSERT INTO `areas` VALUES (\'130223\', \'滦县\', \'130200\'); INSERT INTO `areas` VALUES (\'130224\', \'滦南县\', \'130200\'); INSERT INTO `areas` VALUES (\'130225\', \'乐亭县\', \'130200\'); INSERT INTO `areas` VALUES (\'130227\', \'迁西县\', \'130200\'); INSERT INTO `areas` VALUES (\'130229\', \'玉田县\', \'130200\'); INSERT INTO `areas` VALUES (\'130230\', \'唐海县\', \'130200\'); INSERT INTO `areas` VALUES (\'130281\', \'遵化市\', \'130200\'); INSERT INTO `areas` VALUES (\'130283\', \'迁安市\', \'130200\'); INSERT INTO `areas` VALUES (\'130300\', \'秦皇岛市\', \'130000\'); INSERT INTO `areas` VALUES (\'130301\', \'海港区\', \'130300\'); INSERT INTO `areas` VALUES (\'130303\', \'山海关区\', \'130300\'); INSERT INTO `areas` VALUES (\'130304\', \'北戴河区\', \'130300\'); INSERT INTO `areas` VALUES (\'130321\', \'青龙满族自治县\', \'130300\'); INSERT INTO `areas` VALUES (\'130322\', \'昌黎县\', \'130300\'); INSERT INTO `areas` VALUES (\'130323\', \'抚宁县\', \'130300\'); INSERT INTO `areas` VALUES (\'130324\', \'卢龙县\', \'130300\'); INSERT INTO `areas` VALUES (\'130400\', \'邯郸市\', \'130000\'); INSERT INTO `areas` VALUES (\'130402\', \'邯山区\', \'130400\'); INSERT INTO `areas` VALUES (\'130403\', \'丛台区\', \'130400\'); INSERT INTO `areas` VALUES (\'130404\', \'复兴区\', \'130400\'); INSERT INTO `areas` VALUES (\'130406\', \'峰峰矿区\', \'130400\'); INSERT INTO `areas` VALUES (\'130421\', \'邯郸县\', \'130400\'); INSERT INTO `areas` VALUES (\'130423\', \'临漳县\', \'130400\'); INSERT INTO `areas` VALUES (\'130424\', \'成安县\', \'130400\'); INSERT INTO `areas` VALUES (\'130425\', \'大名县\', \'130400\'); INSERT INTO `areas` VALUES (\'130426\', \'涉县\', \'130400\'); INSERT INTO `areas` VALUES (\'130427\', \'磁县\', \'130400\'); INSERT INTO `areas` VALUES (\'130428\', \'肥乡县\', \'130400\'); INSERT INTO `areas` VALUES (\'130429\', \'永年县\', \'130400\'); INSERT INTO `areas` VALUES (\'130430\', \'邱县\', \'130400\'); INSERT INTO `areas` VALUES (\'130431\', \'鸡泽县\', \'130400\'); INSERT INTO `areas` VALUES (\'130432\', \'广平县\', \'130400\'); INSERT INTO `areas` VALUES (\'130433\', \'馆陶县\', \'130400\'); INSERT INTO `areas` VALUES (\'130434\', \'魏县\', \'130400\'); INSERT INTO `areas` VALUES (\'130435\', \'曲周县\', \'130400\'); INSERT INTO `areas` VALUES (\'130481\', \'武安市\', \'130400\'); INSERT INTO `areas` VALUES (\'130500\', \'邢台市\', \'130000\'); INSERT INTO `areas` VALUES (\'130502\', \'桥东区\', \'130500\'); INSERT INTO `areas` VALUES (\'130503\', \'桥西区\', \'130500\'); INSERT INTO `areas` VALUES (\'130521\', \'邢台县\', \'130500\'); INSERT INTO `areas` VALUES (\'130522\', \'临城县\', \'130500\'); INSERT INTO `areas` VALUES (\'130523\', \'内丘县\', \'130500\'); INSERT INTO `areas` VALUES (\'130524\', \'柏乡县\', \'130500\'); INSERT INTO `areas` VALUES (\'130525\', \'隆尧县\', \'130500\'); INSERT INTO `areas` VALUES (\'130526\', \'任县\', \'130500\'); INSERT INTO `areas` VALUES (\'130527\', \'南和县\', \'130500\'); INSERT INTO `areas` VALUES (\'130528\', \'宁晋县\', \'130500\'); INSERT INTO `areas` VALUES (\'130529\', \'巨鹿县\', \'130529\'); INSERT INTO `areas` VALUES (\'130530\', \'新河县\', \'130500\'); INSERT INTO `areas` VALUES (\'130531\', \'广宗县\', \'130500\'); INSERT INTO `areas` VALUES (\'130532\', \'平乡县\', \'130500\'); INSERT INTO `areas` VALUES (\'130533\', \'威县\', \'130500\'); INSERT INTO `areas` VALUES (\'130534\', \'清河县\', \'130500\'); INSERT INTO `areas` VALUES (\'130535\', \'临西县\', \'130500\'); INSERT INTO `areas` VALUES (\'130581\', \'南宫市\', \'130500\'); INSERT INTO `areas` VALUES (\'130582\', \'沙河市\', \'130500\'); INSERT INTO `areas` VALUES (\'130600\', \'保定市\', \'130000\'); INSERT INTO `areas` VALUES (\'130601\', \'新市区\', \'130600\'); INSERT INTO `areas` VALUES (\'130603\', \'北市区\', \'130600\'); INSERT INTO `areas` VALUES (\'130604\', \'南市区\', \'130600\'); INSERT INTO `areas` VALUES (\'130621\', \'满城县\', \'130600\'); INSERT INTO `areas` VALUES (\'130622\', \'清苑县\', \'130600\'); INSERT INTO `areas` VALUES (\'130623\', \'涞水县\', \'130600\'); INSERT INTO `areas` VALUES (\'130624\', \'阜平县\', \'130600\'); INSERT INTO `areas` VALUES (\'130625\', \'徐水县\', \'130600\'); INSERT INTO `areas` VALUES (\'130626\', \'定兴县\', \'130600\'); INSERT INTO `areas` VALUES (\'130627\', \'唐县\', \'130600\'); INSERT INTO `areas` VALUES (\'130628\', \'高阳县\', \'130600\'); INSERT INTO `areas` VALUES (\'130629\', \'容城县\', \'130600\'); INSERT INTO `areas` VALUES (\'130630\', \'涞源县\', \'130600\'); INSERT INTO `areas` VALUES (\'130631\', \'望都县\', \'130600\'); INSERT INTO `areas` VALUES (\'130632\', \'安新县\', \'130600\'); INSERT INTO `areas` VALUES (\'130633\', \'易县\', \'130600\'); INSERT INTO `areas` VALUES (\'130634\', \'曲阳县\', \'130600\'); INSERT INTO `areas` VALUES (\'130635\', \'蠡县\', \'130600\'); INSERT INTO `areas` VALUES (\'130636\', \'顺平县\', \'130600\'); INSERT INTO `areas` VALUES (\'130637\', \'博野县\', \'130600\'); INSERT INTO `areas` VALUES (\'130638\', \'雄县\', \'130600\'); INSERT INTO `areas` VALUES (\'130681\', \'涿州市\', \'130600\'); INSERT INTO `areas` VALUES (\'130682\', \'定州市\', \'130600\'); INSERT INTO `areas` VALUES (\'130683\', \'安国市\', \'130600\'); INSERT INTO `areas` VALUES (\'130684\', \'高碑店市\', \'130600\'); INSERT INTO `areas` VALUES (\'130685\', \'白沟新城县\', \'130600\'); INSERT INTO `areas` VALUES (\'130700\', \'张家口市\', \'130000\'); INSERT INTO `areas` VALUES (\'130702\', \'桥东区\', \'130700\'); INSERT INTO `areas` VALUES (\'130703\', \'桥西区\', \'130700\'); INSERT INTO `areas` VALUES (\'130705\', \'宣化区\', \'130700\'); INSERT INTO `areas` VALUES (\'130706\', \'下花园区\', \'130700\'); INSERT INTO `areas` VALUES (\'130721\', \'宣化县\', \'130700\'); INSERT INTO `areas` VALUES (\'130722\', \'张北县\', \'130700\'); INSERT INTO `areas` VALUES (\'130723\', \'康保县\', \'130700\'); INSERT INTO `areas` VALUES (\'130724\', \'沽源县\', \'130700\'); INSERT INTO `areas` VALUES (\'130725\', \'尚义县\', \'130700\'); INSERT INTO `areas` VALUES (\'130726\', \'蔚县\', \'130700\'); INSERT INTO `areas` VALUES (\'130727\', \'阳原县\', \'130700\'); INSERT INTO `areas` VALUES (\'130728\', \'怀安县\', \'130700\'); INSERT INTO `areas` VALUES (\'130729\', \'万全县\', \'130700\'); INSERT INTO `areas` VALUES (\'130730\', \'怀来县\', \'130700\'); INSERT INTO `areas` VALUES (\'130731\', \'涿鹿县\', \'130700\'); INSERT INTO `areas` VALUES (\'130732\', \'赤城县\', \'130700\'); INSERT INTO `areas` VALUES (\'130733\', \'崇礼县\', \'130700\'); INSERT INTO `areas` VALUES (\'130800\', \'承德市\', \'130000\'); INSERT INTO `areas` VALUES (\'130802\', \'双桥区\', \'130800\'); INSERT INTO `areas` VALUES (\'130803\', \'双滦区\', \'130800\'); INSERT INTO `areas` VALUES (\'130804\', \'鹰手营子矿区\', \'130800\'); INSERT INTO `areas` VALUES (\'130821\', \'承德县\', \'130800\'); INSERT INTO `areas` VALUES (\'130822\', \'兴隆县\', \'130800\'); INSERT INTO `areas` VALUES (\'130823\', \'平泉县\', \'130800\'); INSERT INTO `areas` VALUES (\'130824\', \'滦平县\', \'130800\'); INSERT INTO `areas` VALUES (\'130825\', \'隆化县\', \'130800\'); INSERT INTO `areas` VALUES (\'130826\', \'丰宁满族自治县\', \'130800\'); INSERT INTO `areas` VALUES (\'130827\', \'宽城满族自治县\', \'130800\'); INSERT INTO `areas` VALUES (\'130828\', \'围场满族蒙古族自治县\', \'130800\'); INSERT INTO `areas` VALUES (\'130900\', \'沧州市\', \'130000\'); INSERT INTO `areas` VALUES (\'130902\', \'新华区\', \'130900\'); INSERT INTO `areas` VALUES (\'130903\', \'运河区\', \'130900\'); INSERT INTO `areas` VALUES (\'130921\', \'沧县\', \'130900\'); INSERT INTO `areas` VALUES (\'130922\', \'青县\', \'130900\'); INSERT INTO `areas` VALUES (\'130923\', \'东光县\', \'130900\'); INSERT INTO `areas` VALUES (\'130924\', \'海兴县\', \'130900\'); INSERT INTO `areas` VALUES (\'130925\', \'盐山县\', \'130900\'); INSERT INTO `areas` VALUES (\'130926\', \'肃宁县\', \'130900\'); INSERT INTO `areas` VALUES (\'130927\', \'南皮县\', \'130900\'); INSERT INTO `areas` VALUES (\'130928\', \'吴桥县\', \'130900\'); INSERT INTO `areas` VALUES (\'130929\', \'献县\', \'130900\'); INSERT INTO `areas` VALUES (\'130930\', \'孟村回族自治县\', \'130900\'); INSERT INTO `areas` VALUES (\'130981\', \'泊头市\', \'130900\'); INSERT INTO `areas` VALUES (\'130982\', \'任丘市\', \'130900\'); INSERT INTO `areas` VALUES (\'130983\', \'黄骅市\', \'130900\'); INSERT INTO `areas` VALUES (\'130984\', \'河间市\', \'130900\'); INSERT INTO `areas` VALUES (\'131000\', \'廊坊市\', \'130000\'); INSERT INTO `areas` VALUES (\'131002\', \'安次区\', \'131000\'); INSERT INTO `areas` VALUES (\'131003\', \'广阳区\', \'131000\'); INSERT INTO `areas` VALUES (\'131022\', \'固安县\', \'131000\'); INSERT INTO `areas` VALUES (\'131023\', \'永清县\', \'131000\'); INSERT INTO `areas` VALUES (\'131024\', \'香河县\', \'131000\'); INSERT INTO `areas` VALUES (\'131025\', \'大城县\', \'131000\'); INSERT INTO `areas` VALUES (\'131026\', \'文安县\', \'131000\'); INSERT INTO `areas` VALUES (\'131028\', \'大厂回族自治县\', \'131000\'); INSERT INTO `areas` VALUES (\'131081\', \'霸州市\', \'131000\'); INSERT INTO `areas` VALUES (\'131082\', \'三河市\', \'131000\'); INSERT INTO `areas` VALUES (\'131100\', \'衡水市\', \'130000\'); INSERT INTO `areas` VALUES (\'131102\', \'桃城区\', \'131100\'); INSERT INTO `areas` VALUES (\'131121\', \'枣强县\', \'131100\'); INSERT INTO `areas` VALUES (\'131122\', \'武邑县\', \'131100\'); INSERT INTO `areas` VALUES (\'131123\', \'武强县\', \'131100\'); INSERT INTO `areas` VALUES (\'131124\', \'饶阳县\', \'131100\'); INSERT INTO `areas` VALUES (\'131125\', \'安平县\', \'131100\'); INSERT INTO `areas` VALUES (\'131126\', \'故城县\', \'131100\'); INSERT INTO `areas` VALUES (\'131127\', \'景县\', \'131100\'); INSERT INTO `areas` VALUES (\'131128\', \'阜城县\', \'131100\'); INSERT INTO `areas` VALUES (\'131181\', \'冀州市\', \'131100\'); INSERT INTO `areas` VALUES (\'131182\', \'深州市\', \'131100\');
由于每个省、市、以及县的数据均在一个表内,这时候我们需要用到自关联,由于省的aid == 市的pid,而市的aid == 县的pid,则:
--- 创建一个表将上面的数据插入 create table areas( aid int primary key, atitle varchar(20), pid int ); 查询所有省份 select * from areas where pid is null; -- 查询出山东省有哪些市 select * from areas as province inner join areas as city on city.pid=province.aid having province.atitle="山东省"; select province.atitle, city.atitle from areas as province inner join areas as city on city.pid=province.aid having province.atitle="山东省"; -- 查询出青岛市有哪些县城 select * from areas as city inner join areas as province on city.pid=province.aid where province.atitle="青岛市";
其实相当于将一个表做两次命名,再做连接查询;
八、子查询
-- 子查询 -- 标量子查询 -- 查询出高于平均身高的信息 select * from students where height > (select avg(height) from students); -- 查询最高的男生信息 select * from students where height = 188; select * from students where height = (select max(height) from students); -- 列级子查询 -- 查询学生的班级号能够对应的学生信息 select * from students where cls_id in (1, 2); select * from students where cls_id in (select id from classes);
主要通过将子查询的结果作为值传入,用做其他地方;
over~~~ 本篇关系某培训机构的的课件~~~~,只是方便记忆~~~~