本篇主要介绍使用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)

创建数据库、数据表

-- 语法结构
select 字段,(聚合函数,表达式)
from 表名
where 条件

...............

order by 字段 desc
limit (第N页-1)*每个的个数

一、条件查询

    -- 消除重复行
-- 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 ('', '北京市', NULL);
INSERT INTO `areas` VALUES ('', '北京市', '');
INSERT INTO `areas` VALUES ('', '东城区', '');
INSERT INTO `areas` VALUES ('', '西城区', '');
INSERT INTO `areas` VALUES ('', '朝阳区', '');
INSERT INTO `areas` VALUES ('', '丰台区', '');
INSERT INTO `areas` VALUES ('', '石景山区', '');
INSERT INTO `areas` VALUES ('', '海淀区', '');
INSERT INTO `areas` VALUES ('', '门头沟区', '');
INSERT INTO `areas` VALUES ('', '房山区', '');
INSERT INTO `areas` VALUES ('', '通州区', '');
INSERT INTO `areas` VALUES ('', '顺义区', '');
INSERT INTO `areas` VALUES ('', '昌平区', '');
INSERT INTO `areas` VALUES ('', '大兴区', '');
INSERT INTO `areas` VALUES ('', '怀柔区', '');
INSERT INTO `areas` VALUES ('', '平谷区', '');
INSERT INTO `areas` VALUES ('', '密云县', '');
INSERT INTO `areas` VALUES ('', '延庆县', '');
INSERT INTO `areas` VALUES ('', '天津市', NULL);
INSERT INTO `areas` VALUES ('', '天津市', '');
INSERT INTO `areas` VALUES ('', '和平区', '');
INSERT INTO `areas` VALUES ('', '河东区', '');
INSERT INTO `areas` VALUES ('', '河西区', '');
INSERT INTO `areas` VALUES ('', '南开区', '');
INSERT INTO `areas` VALUES ('', '河北区', '');
INSERT INTO `areas` VALUES ('', '红桥区', '');
INSERT INTO `areas` VALUES ('', '滨海新区', '');
INSERT INTO `areas` VALUES ('', '东丽区', '');
INSERT INTO `areas` VALUES ('', '西青区', '');
INSERT INTO `areas` VALUES ('', '津南区', '');
INSERT INTO `areas` VALUES ('', '北辰区', '');
INSERT INTO `areas` VALUES ('', '武清区', '');
INSERT INTO `areas` VALUES ('', '宝坻区', '');
INSERT INTO `areas` VALUES ('', '宁河县', '');
INSERT INTO `areas` VALUES ('', '静海县', '');
INSERT INTO `areas` VALUES ('', '蓟县', '');
INSERT INTO `areas` VALUES ('', '河北省', NULL);
INSERT INTO `areas` VALUES ('', '石家庄市', '');
INSERT INTO `areas` VALUES ('', '长安区', '');
INSERT INTO `areas` VALUES ('', '桥东区', '');
INSERT INTO `areas` VALUES ('', '桥西区', '');
INSERT INTO `areas` VALUES ('', '新华区', '');
INSERT INTO `areas` VALUES ('', '井陉矿区', '');
INSERT INTO `areas` VALUES ('', '裕华区', '');
INSERT INTO `areas` VALUES ('', '井陉县', '');
INSERT INTO `areas` VALUES ('', '正定县', '');
INSERT INTO `areas` VALUES ('', '栾城县', '');
INSERT INTO `areas` VALUES ('', '行唐县', '');
INSERT INTO `areas` VALUES ('', '灵寿县', '');
INSERT INTO `areas` VALUES ('', '高邑县', '');
INSERT INTO `areas` VALUES ('', '深泽县', '');
INSERT INTO `areas` VALUES ('', '赞皇县', '');
INSERT INTO `areas` VALUES ('', '无极县', '');
INSERT INTO `areas` VALUES ('', '平山县', '');
INSERT INTO `areas` VALUES ('', '元氏县', '');
INSERT INTO `areas` VALUES ('', '赵县', '');
INSERT INTO `areas` VALUES ('', '辛集市', '');
INSERT INTO `areas` VALUES ('', '藁城市', '');
INSERT INTO `areas` VALUES ('', '晋州市', '');
INSERT INTO `areas` VALUES ('', '新乐市', '');
INSERT INTO `areas` VALUES ('', '鹿泉市', '');
INSERT INTO `areas` VALUES ('', '唐山市', '');
INSERT INTO `areas` VALUES ('', '路南区', '');
INSERT INTO `areas` VALUES ('', '路北区', '');
INSERT INTO `areas` VALUES ('', '古冶区', '');
INSERT INTO `areas` VALUES ('', '开平区', '');
INSERT INTO `areas` VALUES ('', '丰南区', '');
INSERT INTO `areas` VALUES ('', '丰润区', '');
INSERT INTO `areas` VALUES ('', '滦县', '');
INSERT INTO `areas` VALUES ('', '滦南县', '');
INSERT INTO `areas` VALUES ('', '乐亭县', '');
INSERT INTO `areas` VALUES ('', '迁西县', '');
INSERT INTO `areas` VALUES ('', '玉田县', '');
INSERT INTO `areas` VALUES ('', '唐海县', '');
INSERT INTO `areas` VALUES ('', '遵化市', '');
INSERT INTO `areas` VALUES ('', '迁安市', '');
INSERT INTO `areas` VALUES ('', '秦皇岛市', '');
INSERT INTO `areas` VALUES ('', '海港区', '');
INSERT INTO `areas` VALUES ('', '山海关区', '');
INSERT INTO `areas` VALUES ('', '北戴河区', '');
INSERT INTO `areas` VALUES ('', '青龙满族自治县', '');
INSERT INTO `areas` VALUES ('', '昌黎县', '');
INSERT INTO `areas` VALUES ('', '抚宁县', '');
INSERT INTO `areas` VALUES ('', '卢龙县', '');
INSERT INTO `areas` VALUES ('', '邯郸市', '');
INSERT INTO `areas` VALUES ('', '邯山区', '');
INSERT INTO `areas` VALUES ('', '丛台区', '');
INSERT INTO `areas` VALUES ('', '复兴区', '');
INSERT INTO `areas` VALUES ('', '峰峰矿区', '');
INSERT INTO `areas` VALUES ('', '邯郸县', '');
INSERT INTO `areas` VALUES ('', '临漳县', '');
INSERT INTO `areas` VALUES ('', '成安县', '');
INSERT INTO `areas` VALUES ('', '大名县', '');
INSERT INTO `areas` VALUES ('', '涉县', '');
INSERT INTO `areas` VALUES ('', '磁县', '');
INSERT INTO `areas` VALUES ('', '肥乡县', '');
INSERT INTO `areas` VALUES ('', '永年县', '');
INSERT INTO `areas` VALUES ('', '邱县', '');
INSERT INTO `areas` VALUES ('', '鸡泽县', '');
INSERT INTO `areas` VALUES ('', '广平县', '');
INSERT INTO `areas` VALUES ('', '馆陶县', '');
INSERT INTO `areas` VALUES ('', '魏县', '');
INSERT INTO `areas` VALUES ('', '曲周县', '');
INSERT INTO `areas` VALUES ('', '武安市', '');
INSERT INTO `areas` VALUES ('', '邢台市', '');
INSERT INTO `areas` VALUES ('', '桥东区', '');
INSERT INTO `areas` VALUES ('', '桥西区', '');
INSERT INTO `areas` VALUES ('', '邢台县', '');
INSERT INTO `areas` VALUES ('', '临城县', '');
INSERT INTO `areas` VALUES ('', '内丘县', '');
INSERT INTO `areas` VALUES ('', '柏乡县', '');
INSERT INTO `areas` VALUES ('', '隆尧县', '');
INSERT INTO `areas` VALUES ('', '任县', '');
INSERT INTO `areas` VALUES ('', '南和县', '');
INSERT INTO `areas` VALUES ('', '宁晋县', '');
INSERT INTO `areas` VALUES ('', '巨鹿县', '');
INSERT INTO `areas` VALUES ('', '新河县', '');
INSERT INTO `areas` VALUES ('', '广宗县', '');
INSERT INTO `areas` VALUES ('', '平乡县', '');
INSERT INTO `areas` VALUES ('', '威县', '');
INSERT INTO `areas` VALUES ('', '清河县', '');
INSERT INTO `areas` VALUES ('', '临西县', '');
INSERT INTO `areas` VALUES ('', '南宫市', '');
INSERT INTO `areas` VALUES ('', '沙河市', '');
INSERT INTO `areas` VALUES ('', '保定市', '');
INSERT INTO `areas` VALUES ('', '新市区', '');
INSERT INTO `areas` VALUES ('', '北市区', '');
INSERT INTO `areas` VALUES ('', '南市区', '');
INSERT INTO `areas` VALUES ('', '满城县', '');
INSERT INTO `areas` VALUES ('', '清苑县', '');
INSERT INTO `areas` VALUES ('', '涞水县', '');
INSERT INTO `areas` VALUES ('', '阜平县', '');
INSERT INTO `areas` VALUES ('', '徐水县', '');
INSERT INTO `areas` VALUES ('', '定兴县', '');
INSERT INTO `areas` VALUES ('', '唐县', '');
INSERT INTO `areas` VALUES ('', '高阳县', '');
INSERT INTO `areas` VALUES ('', '容城县', '');
INSERT INTO `areas` VALUES ('', '涞源县', '');
INSERT INTO `areas` VALUES ('', '望都县', '');
INSERT INTO `areas` VALUES ('', '安新县', '');
INSERT INTO `areas` VALUES ('', '易县', '');
INSERT INTO `areas` VALUES ('', '曲阳县', '');
INSERT INTO `areas` VALUES ('', '蠡县', '');
INSERT INTO `areas` VALUES ('', '顺平县', '');
INSERT INTO `areas` VALUES ('', '博野县', '');
INSERT INTO `areas` VALUES ('', '雄县', '');
INSERT INTO `areas` VALUES ('', '涿州市', '');
INSERT INTO `areas` VALUES ('', '定州市', '');
INSERT INTO `areas` VALUES ('', '安国市', '');
INSERT INTO `areas` VALUES ('', '高碑店市', '');
INSERT INTO `areas` VALUES ('', '白沟新城县', '');
INSERT INTO `areas` VALUES ('', '张家口市', '');
INSERT INTO `areas` VALUES ('', '桥东区', '');
INSERT INTO `areas` VALUES ('', '桥西区', '');
INSERT INTO `areas` VALUES ('', '宣化区', '');
INSERT INTO `areas` VALUES ('', '下花园区', '');
INSERT INTO `areas` VALUES ('', '宣化县', '');
INSERT INTO `areas` VALUES ('', '张北县', '');
INSERT INTO `areas` VALUES ('', '康保县', '');
INSERT INTO `areas` VALUES ('', '沽源县', '');
INSERT INTO `areas` VALUES ('', '尚义县', '');
INSERT INTO `areas` VALUES ('', '蔚县', '');
INSERT INTO `areas` VALUES ('', '阳原县', '');
INSERT INTO `areas` VALUES ('', '怀安县', '');
INSERT INTO `areas` VALUES ('', '万全县', '');
INSERT INTO `areas` VALUES ('', '怀来县', '');
INSERT INTO `areas` VALUES ('', '涿鹿县', '');
INSERT INTO `areas` VALUES ('', '赤城县', '');
INSERT INTO `areas` VALUES ('', '崇礼县', '');
INSERT INTO `areas` VALUES ('', '承德市', '');
INSERT INTO `areas` VALUES ('', '双桥区', '');
INSERT INTO `areas` VALUES ('', '双滦区', '');
INSERT INTO `areas` VALUES ('', '鹰手营子矿区', '');
INSERT INTO `areas` VALUES ('', '承德县', '');
INSERT INTO `areas` VALUES ('', '兴隆县', '');
INSERT INTO `areas` VALUES ('', '平泉县', '');
INSERT INTO `areas` VALUES ('', '滦平县', '');
INSERT INTO `areas` VALUES ('', '隆化县', '');
INSERT INTO `areas` VALUES ('', '丰宁满族自治县', '');
INSERT INTO `areas` VALUES ('', '宽城满族自治县', '');
INSERT INTO `areas` VALUES ('', '围场满族蒙古族自治县', '');
INSERT INTO `areas` VALUES ('', '沧州市', '');
INSERT INTO `areas` VALUES ('', '新华区', '');
INSERT INTO `areas` VALUES ('', '运河区', '');
INSERT INTO `areas` VALUES ('', '沧县', '');
INSERT INTO `areas` VALUES ('', '青县', '');
INSERT INTO `areas` VALUES ('', '东光县', '');
INSERT INTO `areas` VALUES ('', '海兴县', '');
INSERT INTO `areas` VALUES ('', '盐山县', '');
INSERT INTO `areas` VALUES ('', '肃宁县', '');
INSERT INTO `areas` VALUES ('', '南皮县', '');
INSERT INTO `areas` VALUES ('', '吴桥县', '');
INSERT INTO `areas` VALUES ('', '献县', '');
INSERT INTO `areas` VALUES ('', '孟村回族自治县', '');
INSERT INTO `areas` VALUES ('', '泊头市', '');
INSERT INTO `areas` VALUES ('', '任丘市', '');
INSERT INTO `areas` VALUES ('', '黄骅市', '');
INSERT INTO `areas` VALUES ('', '河间市', '');
INSERT INTO `areas` VALUES ('', '廊坊市', '');
INSERT INTO `areas` VALUES ('', '安次区', '');
INSERT INTO `areas` VALUES ('', '广阳区', '');
INSERT INTO `areas` VALUES ('', '固安县', '');
INSERT INTO `areas` VALUES ('', '永清县', '');
INSERT INTO `areas` VALUES ('', '香河县', '');
INSERT INTO `areas` VALUES ('', '大城县', '');
INSERT INTO `areas` VALUES ('', '文安县', '');
INSERT INTO `areas` VALUES ('', '大厂回族自治县', '');
INSERT INTO `areas` VALUES ('', '霸州市', '');
INSERT INTO `areas` VALUES ('', '三河市', '');
INSERT INTO `areas` VALUES ('', '衡水市', '');
INSERT INTO `areas` VALUES ('', '桃城区', '');
INSERT INTO `areas` VALUES ('', '枣强县', '');
INSERT INTO `areas` VALUES ('', '武邑县', '');
INSERT INTO `areas` VALUES ('', '武强县', '');
INSERT INTO `areas` VALUES ('', '饶阳县', '');
INSERT INTO `areas` VALUES ('', '安平县', '');
INSERT INTO `areas` VALUES ('', '故城县', '');
INSERT INTO `areas` VALUES ('', '景县', '');
INSERT INTO `areas` VALUES ('', '阜城县', '');
INSERT INTO `areas` VALUES ('', '冀州市', '');
INSERT INTO `areas` VALUES ('', '深州市', '');

某几个省市县的数据

  由于每个省、市、以及县的数据均在一个表内,这时候我们需要用到自关联,由于省的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~~~ 本篇关系某培训机构的的课件~~~~,只是方便记忆~~~~

三:MySQL系列之SQL查询的更多相关文章

  1. mysql系列-⼀条SQL查询语句是如何执⾏的?

    ⼀条SQL查询语句是如何执⾏的? ⼤体来说,MySQL 可以分为 Server 层和存储引擎层两部分 Server 层 Server 层包括连接器.查询缓存.分析器.优化器.执⾏器等,涵盖 MySQL ...

  2. MySQL系列(七)--SQL优化的步骤

    前面讲了如何设计数据库表结构.存储引擎.索引优化等内存,这篇文章会讲述如何进行SQL优化,也是面试中关于数据库肯定会被问到的, 这些内容不仅仅是为了面试,更重要的是付诸实践,最终用到工作当中 之前的M ...

  3. mysql体系结构和sql查询执行过程简析

    一: mysql体系结构 1)Connectors 不同语言与 SQL 的交互 2)Management Serveices & Utilities 系统管理和控制工具 备份和恢复的安全性,复 ...

  4. Python全栈 MySQL 数据库 (SQL查询、备份、恢复、授权)

    ParisGabriel              每天坚持手写  一天一篇  决定坚持几年 为了梦想为了信仰    开局一张图   今天接着昨天的说   索引有4种:      普通 索引 :ind ...

  5. Mysql系列一:SQL入门

    csdn博客搬迁 连接数据库:1.在dos窗口下,进入数据库的安装目录的bin目录下,使用mysqld命令启动数据库服务,或者在计算机的服务里面启动mysql服务2.另外打开一个dos窗口,进入数据库 ...

  6. 二:MySQL系列之SQL基本操作(二)

    本篇主要介绍SOL语句的基本操作,主要有分为 连接数据库,创建数据库.创建数据表.添加数据记录,基本的查询功能等操作. 一.针对数据库的操作 -- 1.连接数据库 mysql -uroot -p my ...

  7. 【SQL Server 学习系列】-- SQL查询数据库表字段值不为空或Null的所有列

    ) set @TableName = 'Agency' -- 表名 declare @querySql nvarchar(max) set @querySql = 'select ' ) declar ...

  8. mysql 变量定义 sql查询

    SET @idnoStr:='"idNo":"'; SELECT LOCATE(@idnoStr, param_array), LOCATE('",', par ...

  9. mysql数据库使用sql查询数据库大小及表大小

    网上查了很多资料,最后发现一个可行的,分享如下: 数据库大小查询: select concat(round(sum(DATA_LENGTH/1024/1024),2),'M') from inform ...

随机推荐

  1. 教孩子学编程 Python

    教孩子学编程   Python 目录 第1 章 Python 基础:认识环境 111 认识Python 312 用Python 编写程序 513 运行Python 程序 514 本章小结 615 编程 ...

  2. 【shell比较字符串】

    if [ 'AAA' = 'ABC' ]; then echo "the same" else echo "not the same" fi shell比较字符 ...

  3. [转]C++ STL中的Binary search(二分查找)

    链接地址:https://www.cnblogs.com/wkfvawl/p/9475939.html

  4. IE浏览器提示打印控件未安装的一些原因

    打印控件未安装!点击这里执行安装,安装后请刷新页面或重新进入.--该提示是写在LodopFuncs.js里的.相关本博客其他博文:提示“Web打印服务CLodop未安装启动”的各种原因和解决方法.C- ...

  5. Input.GetMouseButtonDown 在fixedupdate中会出现丢失问题,在update中则完全没这个问题

    Input.GetMouseButtonDown 在fixedupdate中会出现丢失问题,在update中则完全没这个问题

  6. 使用 LVS 实现负载均衡原理及安装配置详解(课堂随笔)

    一.负载均衡LVS基本介绍 LB集群的架构和原理很简单,就是当用户的请求过来时,会直接分发到Director Server上,然后它把用户的请求根据设置好的调度算法,智能均衡地分发到后端真正服务器(r ...

  7. 在ensp上配置通过Stelnet登录系统

    我们为什么我们要采用Stelent登录? 因为不安全,我们要采用更加安全的方式,双向加密,通过ssh网络安全协议 下面我们开始实验:使用路由器R1模拟PC,作为SSH的客户端:路由器R2作为SSH的服 ...

  8. LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)

    94. 二叉树的中序遍历 94. Binary Tree Inorder Traversal 题目描述 给定一个二叉树,返回它的 中序 遍历. LeetCode94. Binary Tree Inor ...

  9. 使用TypeScript创建React Native

    ⒈初始化 React Native环境 参考https://reactnative.cn/docs/getting-started.html ⒉安装React Native官方的脚手架工具 npm i ...

  10. PAT(B) 1068 万绿丛中一点红(C)

    题目链接:1068 万绿丛中一点红 (20 point(s)) 参考博客:1068. 万绿丛中一点红(20) i逆天耗子丶 题目描述 对于计算机而言,颜色不过是像素点对应的一个 24 位的数值.现给定 ...