1. 基本查询

  查询基本使用包括:条件、排序、聚合函数、分组和分页。

实例详解查询

1> 创建students表用作实验

MariaDB [testdb]> drop table students;                    #删除上节创建的表
MariaDB [testdb]> create table students ( #创建students表
-> id int unsigned not null auto_increment primary key,
-> name varchar() default '',
-> age tinyint unsigned default ,
-> high decimal(,),
-> gender enum('男', '女', '中性', '保密') default '保密',
-> cls_id int unsigned default ,
-> is_delete bit default
-> );
MariaDB [testdb]> show table students; #给表里面写入数据
ERROR (): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'students' at line
MariaDB [testdb]> insert into students values
-> (,'小明',,180.00,,,),
-> (,'小月月',,180.00,,,),
-> (,'彭于晏',,185.00,,,),
-> (,'刘德华',,175.00,,,),
-> (,'黄蓉',,160.00,,,),
-> (,'凤姐',,150.00,,,),
-> (,'王祖贤',,170.00,,,),
-> (,'周杰伦儿',,null,,,),
-> (,'程坤',,181.00,,,),
-> (,'和珅',,166.00,,,),
-> (,'刘亦菲',,162.00,,,),
-> (,'金星',,180.00,,,),
-> (,'静香',,170.00,,,),
-> (,'郭靖',,167.00,,,),
-> (,'周杰',,178.00,,,),
-> (,'钱小豪',,178.00,,,),
-> (,'谢霆锋',,175.00,,,),
-> (,'陈冠希',,175.00,,,);

2> 查询

查询所有列(字段)

  格式: select * from 表名

MariaDB [testdb]> select * from students;
+----+---------------+-- ---+-------+----------+--------+---------+
| id | name | age | high | gender | cls_id |is_delete |
+----+--------------+----- -+--------+--------+--------+----------+
| | 小明 | | 180.00 | 男 | | |
| | 小月月 | | 180.00 | 男 | | |
| | 彭于晏 | | 185.00 | 男 | | |
| | 刘德华 | | 175.00 | 男 | | |
| | 黄蓉 | | 160.00 | 女 | | |
| | 凤姐 | | 150.00 | 保密 | | |
| | 王祖贤 | | 170.00 | 女 | | |
| | 周杰伦儿 | | NULL | 男 | | |
| | 程坤 | | 181.00 | 男 | | |
| | 和珅 | | 166.00 | 男 | | |
| | 刘亦菲 | | 162.00 | 女 | | |
| | 金星 | | 180.00 | 中性 | | |
| | 静香 | | 170.00 | 女 | | |
| | 郭靖 | | 167.00 | 男 | | |
| | 周杰 | | 178.00 | 男 | | |
| | 钱小豪 | | 178.00 | 男 | | |
| | 谢霆锋 | | 175.00 | 男 | | |
| | 陈冠希 | | 175.00 | 男 | | |
+----+---------- --+------+--------+--------+-------+------------+
rows in set (0.001 sec)

一定条件查询:
  用条件语句where,后面跟条件。

MariaDB [testdb]> select * from students where id>;  #支持条件表达式、逻辑语言and、&&、or、||
+----+-----------+------+--------+--------+---------+-----------+
| id | name | age | high | gender | cls_id | is_delete |
+----+-----------+------+--------+--------+---------+-----------+
| | 凤姐 | | 150.00 | 保密 | | |
| | 王祖贤 | | 170.00 | 女 | | |
| | 周杰伦儿 | | NULL | 男 | | |
| | 程坤 | | 181.00 | 男 | | |
| | 和珅 | | 166.00 | 男 | | |
| | 刘亦菲 | | 162.00 | 女 | | |
| | 金星 | | 180.00 | 中性 | | |
| | 静香 | | 170.00 | 女 | | |
| | 郭靖 | | 167.00 | 男 | | |
| | 周杰 | | 178.00 | 男 | | |
| | 钱小豪 | | 178.00 | 男 | | |
| | 谢霆锋 | | 175.00 | 男 | | |
| | 陈冠希 | | 175.00 | 男 | 1 | |
+----+----------+------+---------+--------+---------+-----------+

select * from语句虽然简单,但对于企业来说,数据量特别大,使用该命令会列出所有的数据,严重影响内存。一般不直接用该命令。

3> 查看指定列

MariaDB [testdb]> select id,name from students;
+----+--------------+
| id | name |
+----+--------------+
| | 小明 |
| | 小月月 |
| | 彭于晏 |
| | 刘德华 |
| | 黄蓉 |
| | 凤姐 |
| | 王祖贤 |
| | 周杰伦儿 |
| | 程坤 |
| | 和珅 |
| | 刘亦菲 |
| | 金星 |
| | 静香 |
| | 郭靖 |
| | 周杰 |
| | 钱小豪 |
| | 谢霆锋 |
| | 陈冠希 |
+----+--------------+

4>通过表名查询

大量含有相同字段的不同类型的表查询时容易混淆,因此加表名进行查询得到的结果比较合理。

MariaDB [testdb]> select students.id,students.name from students;
+----+--------------+
| id | name |
+----+--------------+
| | 小明 |
| | 小月月 |
| | 彭于晏 |
| | 刘德华 |
| | 黄蓉 |
| | 凤姐 |
| | 王祖贤 |
| | 周杰伦儿 |
| | 程坤 |
| | 和珅 |
| | 刘亦菲 |
| | 金星 |
| | 静香 |
| | 郭靖 |
| | 周杰 |
| | 钱小豪 |
| | 谢霆锋 |
| | 陈冠希 |

5> 给表起别名查询

  使用as

MariaDB [testdb]> select s.id,s.name from students as s;
+----+--------------+
| id | name |
+----+--------------+
| | 小明 |
| | 小月月 |
| | 彭于晏 |
| | 刘德华 |
| | 黄蓉 |
| | 凤姐 |
| | 王祖贤 |
| | 周杰伦儿 |
| | 程坤 |
| | 和珅 |
| | 刘亦菲 |
| | 金星 |
| | 静香 |
| | 郭靖 |
| | 周杰 |
| | 钱小豪 |
| | 谢霆锋 |
| | 陈冠希 |
+----+-------------+

6>消除重复行查询

对于students表的age列有重复,可以用distinct去重复查询

MariaDB [testdb]> select distinct age from students;
+------+
| age |
+------+
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |

7> 条件查询

#查询年纪大于18岁的信息
MariaDB [testdb]> select * from students where age > ;
+----+--------------+------+--------+--------+--------+-----------+
| id | name | age | high | gender | cls_id | is_delete |
+----+--------------+------+--------+--------+--------+-----------+
| | 小月月 | | 180.00 | 男 | | |
| | 彭于晏 | | 185.00 | 男 | | |
| | 刘德华 | | 175.00 | 男 | | |
| | 黄蓉 | | 160.00 | 女 | | |
| | 凤姐 | | 150.00 | 保密 | | |
| | 王祖贤 | | 170.00 | 女 | | |
| | 周杰伦儿 | | NULL | 男 | | |
| | 程坤 | | 181.00 | 男 | | |
| | 和珅 | | 166.00 | 男 | | |
| | 刘亦菲 | | 162.00 | 女 | | |
| | 金星 | | 180.00 | 中性 | | |
| | 郭靖 | | 167.00 | 男 | | |
| | 周杰 | | 178.00 | 男 | | |
| | 钱小豪 | | 178.00 | 男 | | |
| | 谢霆锋 | | 175.00 | 男 | | |
| | 陈冠希 | | 175.00 | 男 | | |
+----+--------------+------+--------+--------+--------+-----------+ #查询年龄介于18岁到28岁之间的
MariaDB [testdb]> select * from students where age between and ;
MariaDB [testdb]> select * from students where age>= and age<=;
+----+-----------+------+--------+--------+--------+-----------+
| id | name | age | high | gender | cls_id | is_delete |
+----+-----------+------+--------+--------+--------+-----------+
| | 小明 | | 180.00 | 男 | | |
| | 小月月 | | 180.00 | 男 | | |
| | 彭于晏 | | 185.00 | 男 | | |
| | 静香 | 18 | 170.00 | 女 | | |
| | 郭靖 | | 167.00 | 男 | | | #查询年龄18岁以上或者身高180上
MariaDB [testdb]> select * from students where age> or high>;
+----+---------------+------+--------+--------+--------+-----------+
| id | name | age | high |gender |cls_id |is_delete |
+----+---------------+------+--------+-------+---------+-----------+
| | 小月月 | | 180.00 | 男 | | |
| | 彭于晏 | | 185.00 | 男 | | |
| | 刘德华 | | 175.00 | 男 | | |
| | 黄蓉 | | 160.00 | 女 | | |
| | 凤姐 | | 150.00 | 保密 | | |
| | 王祖贤 | | 170.00 | 女 | | |
| | 周杰伦儿 | | NULL | 男 | | |
| | 程坤 | | 181.00 | 男 | | |
| | 和珅 | | 166.00 | 男 | | |
| | 刘亦菲 | | 162.00 | 女 | | |
| | 金星 | | 180.00 | 中性 | | |
| | 郭靖 | | 167.00 | 男 | | |
| | 周杰 | | 178.00 | 男 | | |
| | 钱小豪 | | 178.00 | 男 | | |
| | 谢霆锋 | | 175.00 | 男 | | |
| | 陈冠希 | | 175.00 | 男 | | |
+----+--------------+------+--------+---------+--------+----------+

8> 模糊查询

like

%    替代0个或多个,与shell里的命令*相同

__    代表一个字符

#查询名字中带有锋的数据
MariaDB [testdb]> select * from students where name like '%锋%';
+----+-----------+------+---------+--------+--------+-----------+
| id | name | age | high | gender | cls_id | is_delete |
+----+-----------+------+---------+--------+--------+-----------+
| | 谢霆锋 | | 175.00 | 男 | | |
+----+-----------+------+---------+--------+--------+-----------+ #查询名字是两个字的数据
MariaDB [testdb]> select * from students where name like '__';
+----+--------+------+--------+--------+--------+-----------+
| id | name | age | high | gender | cls_id | is_delete |
+----+--------+------+--------+--------+--------+-----------+
| | 小明 | | 180.00 | 男 | | |
| | 黄蓉 | | 160.00 | 女 | | |
| | 凤姐 | | 150.00 | 保密 | | |
| | 程坤 | | 181.00 | 男 | | |
| | 和珅 | | 166.00 | 男 | | |
| | 金星 | | 180.00 | 中性 | | |
| | 静香 | | 170.00 | 女 | | |
| | 郭靖 | | 167.00 | 男 | | |
| | 周杰 | | 178.00 | 男 | | |
+----+--------+------+--------+--------+--------+-----------+ #查找名字至少三个字以上的数据
MariaDB [testdb]> select * from students where name like '___%';
+----+--------------+------+--------+--------+--------+-----------+
| id | name | age | high | gender | cls_id | is_delete |
+----+--------------+------+--------+--------+--------+-----------+
| | 小月月 | | 180.00 | 男 | | |
| | 彭于晏 | | 185.00 | 男 | | |
| | 刘德华 | | 175.00 | 男 | | |
| | 王祖贤 | | 170.00 | 女 | | |
| | 周杰伦儿 | | NULL | 男 | | |
| | 刘亦菲 | | 162.00 | 女 | | |
| | 钱小豪 | | 178.00 | 男 | | |
| | 谢霆锋 | | 175.00 | 男 | | |
| | 陈冠希 | | 175.00 | 男 | | |
+----+--------------+------+--------+--------+--------+-----------+

#查询名字中带有锋的数据
MariaDB [testdb]> select * from students where name like '%锋%';
+----+-----------+------+---------+--------+--------+-----------+
| id | name | age | high | gender | cls_id | is_delete |
+----+-----------+------+---------+--------+--------+-----------+
| | 谢霆锋 | | 175.00 | 男 | | |
+----+-----------+------+---------+--------+--------+-----------+ #查询名字是两个字的数据
MariaDB [testdb]> select * from students where name like '__';
+----+--------+------+--------+--------+--------+-----------+
| id | name | age | high | gender | cls_id | is_delete |
+----+--------+------+--------+--------+--------+-----------+
| | 小明 | | 180.00 | 男 | | |
| | 黄蓉 | | 160.00 | 女 | | |
| | 凤姐 | | 150.00 | 保密 | | |
| | 程坤 | | 181.00 | 男 | | |
| | 和珅 | | 166.00 | 男 | | |
| | 金星 | | 180.00 | 中性 | | |
| | 静香 | | 170.00 | 女 | | |
| | 郭靖 | | 167.00 | 男 | | |
| | 周杰 | | 178.00 | 男 | | |
+----+--------+------+--------+--------+--------+-----------+ #查找名字至少三个字以上的数据
MariaDB [testdb]> select * from students where name like '___%';
+----+--------------+------+--------+--------+--------+-----------+
| id | name | age | high | gender | cls_id | is_delete |
+----+--------------+------+--------+--------+--------+-----------+
| | 小月月 | | 180.00 | 男 | | |
| | 彭于晏 | | 185.00 | 男 | | |
| | 刘德华 | | 175.00 | 男 | | |
| | 王祖贤 | | 170.00 | 女 | | |
| | 周杰伦儿 | | NULL | 男 | | |
| | 刘亦菲 | | 162.00 | 女 | | |
| | 钱小豪 | | 178.00 | 男 | | |
| | 谢霆锋 | | 175.00 | 男 | | |
| | 陈冠希 | | 175.00 | 男 | | |
+----+--------------+------+--------+--------+--------+-----------+

9> 范围查询

  in (1,3,8)表示在一个非连续的范围内

  between…and

  not between…and   表示否定

#查询年龄为18、34、58的人
MariaDB [testdb]> select * from students where age in(,,);
+----+--------------+------+--------+--------+--------+-----------+
| id | name | age | high | gender | cls_id | is_delete |
+----+--------------+------+--------+--------+--------+-----------+
| | 小明 | | 180.00 | 男 | | |
| | 刘德华 | | 175.00 | 男 | | |
| | 周杰伦儿 | | NULL | 男 | | |
| | 静香 | | 170.00 | 女 | | |
+----+--------------+-------+--------+-------+--------+-----------+ #查询年龄不在18到34岁的人
MariaDB [testdb]> select * from students where age not between and ;
+----+--------+------+--------+--------+--------+-----------+
| id | name | age | high | gender | cls_id | is_delete |
+----+--------+------+--------+--------+--------+-----------+
| | 刘德华 | | 175.00 | 男 | | |
| | 黄蓉 | | 160.00 | 女 | | |
| | 凤姐 | | 150.00 | 保密 | | |
| | 王祖贤 | | 170.00 | 女 | | |
| | 程坤 | | 181.00 | 男 | | |
| | 和珅 | | 166.00 | 男 | | |
| | 金星 | | 180.00 | 中性 | | |
| | 钱小豪 | | 178.00 | 男 | | |
| | 谢霆锋 | | 175.00 | 男 | | |
| | 陈冠希 | | 175.00 | 男 | | |

10> 空判断

  判断is null

#查询身高为空的数据信息
MariaDB [testdb]> select * from students where high is null;
+----+--------------+------+------+--------+--------+-----------+
| id | name | age | high | gender | cls_id | is_delete |
+----+--------------+------+------+--------+--------+-----------+
| | 周杰伦儿 | | NULL | 男 | | |
+----+--------------+------+------+--------+--------+-----------+

11>排序

  order by 字段;

  asc从小到大排列,即升序,默认为此排序;

  desc从大到小排序,即降序;

#按照年纪从大到小排序
MariaDB [testdb]> select * from students order by age desc;
+----+--------------+------+--------+--------+--------+-----------+
| id | name | age | high | gender | cls_id | is_delete |
+----+--------------+------+--------+--------+--------+-----------+
| | 黄蓉 | | 160.00 | 女 | | |
| | 刘德华 | | 175.00 | 男 | | |
| | 钱小豪 | | 178.00 | 男 | | |
| | 和珅 | | 166.00 | 男 | | |
| | 王祖贤 | | 170.00 | 女 | | |
| | 金星 | | 180.00 | 中性 | | |
| | 凤姐 | | 150.00 | 保密 | | |
| | 程坤 | | 181.00 | 男 | | |
| | 谢霆锋 | | 175.00 | 男 | | |
| | 陈冠希 | | 175.00 | 男 | | |
| | 周杰伦儿 | | NULL | 男 | | |
| | 周杰 | | 178.00 | 男 | | |
| | 刘亦菲 | | 162.00 | 女 | | |
| | 彭于晏 | | 185.00 | 男 | | |
| | 郭靖 | | 167.00 | 男 | | |
| | 小月月 | | 180.00 | 男 | | |
| | 静香 | | 170.00 | 女 | | |
| | 小明 | | 180.00 | 男 | | |
+----+-------------+------+------- -+-------+--------+-----------+ #查询年纪在18到34 岁之间的男性,按照年纪从大到小排序 MariaDB [testdb]> select * from students where gender= and age between and order by age desc; #可读性差
MariaDB [testdb]> select * from students where (age between and ) and gender= order by age desc;
+----+--------------+------+--------+--------+--------+-----------+
| id | name | age | high | gender | cls_id | is_delete |
+----+--------------+------+--------+--------+--------+-----------+
| | 周杰伦儿 | | NULL | 男 | | |
| | 周杰 | | 178.00 | 男 | | |
| | 彭于晏 | | 185.00 | 男 | | |
| | 郭靖 | | 167.00 | 男 | | |
| | 小月月 | | 180.00 | 男 | | |
| | 小明 | | 180.00 | 男 | | |
+----+--------------+------+--------+--------+--------+-----------+ #order by 多字段
#按年龄从打大小后再按身高从大到小排序
MariaDB [testdb]> select * from students order by age desc,high desc;
+----+----------+------+--------+--------+--------+-----------+
| id | name | age | high | gender | cls_id | is_delete |
+----+----------+------+--------+--------+--------+-----------+
| | 黄蓉 | | 160.00 | 女 | | |
| | 刘德华 | | 175.00 | 男 | | |
| | 钱小豪 | | 178.00 | 男 | | |
| | 和珅 | | 166.00 | 男 | | |
| | 王祖贤 | | 170.00 | 女 | | |
| | 金星 | | 180.00 | 中性 | | |
| | 程坤 | | 181.00 | 男 | | |
| | 凤姐 | | 150.00 | 保密 | | |
| | 谢霆锋 | | 175.00 | 男 | | |
| | 陈冠希 | | 175.00 | 男 | | |
| | 周杰伦儿 | | NULL | 男 | | |
| | 周杰 | | 178.00 | 男 | | |
| | 刘亦菲 | | 162.00 | 女 | | |
| | 彭于晏 | | 185.00 | 男 | | |
| | 郭靖 | | 167.00 | 男 | | |
| | 小月月 | | 180.00 | 男 | | |
| | 小明 | | 180.00 | 男 | | |
| | 静香 | | 170.00 | 女 | | |
+----+----------+------+--------+--------+--------+-----------+

12> 聚合函数

总数(量) count

最大值 max

最小值 min

平均数avg

#统计总共有多少条数据
MariaDB [testdb]> select count(*) from students; #*号统计的数据最准确,count的括号里可以跟字段用来统计某个字段共有多少条有效数据数据,
+----------+
| count(*) |
+----------+
| |
+----------+ #查询总共有多少女性数据
MariaDB [testdb]> select count(*) from students where gender=;
+----------+
| count(*) |
+----------+
| |
+----------+
row in set (0.000 sec) #查看年龄最大的数据
MariaDB [testdb]> select max(age) from students;
+----------+
| max(age) |
+----------+
| |
+----------+ #查看年纪最大的人名
MariaDB [testdb]> select name,max(age) from students; #这样统计出来是错的
+--------+----------+
| name | max(age) |
+--------+----------+
| 小明 | |
+--------+----------+
#可以从大到小分组之后分页选出第一条数据,即可。 #查看女性的最高身高
MariaDB [testdb]> select max(high) from students where gender=;
+-----------+
| max(high) |
+-----------+
| 70.00 | #求年纪和并设置别名
MariaDB [testdb]> select sum(age) as '年纪总和' from students;
+--------------+
| 年纪总和 |
+--------------+
| |
+--------------+ #求年纪的平均和
MariaDB [testdb]> select avg(age) as '平均年龄' from students;
MariaDB [testdb]> select sum(age)/count(*) as '平均年纪' from students;
+--------------+
| 平均年纪 |
+--------------+
| 41.0556 |
+--------------+ row in set (0.001 sec) #保留年纪两位小数,用round函数
MariaDB [testdb]> select round(sum(age)/count(*),) as '平均年纪' from students;
+--------------+
| 平均年纪 |
+--------------+
| 41.06 |
+--------------+

聚合函数通常是和分组一起使用的。

13> 分组

  group by

  having函数

#按性别(gender)分组
MariaDB [testdb]> select gender from students group by gender; #第一个gender为组名
+--------+
| gender |
+--------+
| 男 |
| 女 |
| 中性 |
| 保密 |
+-------+ #显示每组人的个数
MariaDB [testdb]> select gender,count(*) from students group by gender;
+---------+----------+
| gender | count(*) |
+---------+----------+
| 男 | |
| 女 | |
| 中性 | |
| 保密 | |
+--------+------------+ #查看男性组中成员姓名
MariaDB [testdb]> select gender,group_concat(name) from students where gender= group by gender; #group_gender 按组显示 #查看每组成员的name、age、high,用空格分割(自定义分隔符)
MariaDB [testdb]> select gender,group_concat(name,' ',age,' ',high) from students group by gender; #分隔符加单引号,可以自定义分隔符,如/、|等
| 男 | 小明 180.00,谢霆锋 175.00,钱小豪 178.00,周杰 178.00,郭靖 167.00,和珅 166.00,程坤 181.00,陈冠希 175.00,小月月 180.00,彭于晏 185.00,刘德华 175.00 |
| 女 | 黄蓉 160.00,静香 170.00,刘亦菲 162.00,王祖贤 170.00 |
| 中性 | 金星 180.00 |
| 保密 | 凤姐 150.00 #看到group by分组,用条件表达式首先应想到用函数having,而不是where。
#查询平均年纪大于40 的组
MariaDB [testdb]> select gender,avg(age) from students group by gender having avg(age)>;
+--------+----------+
| gender | avg(age) |
+--------+----------+
| 女 | 51.7500 |
| 中性 | 45.0000 |
| 保密 | 44.0000 |
+--------+----------+ #查看人数多于4个的组
MariaDB [testdb]> select gender,group_concat(name) from students group by gender having count(*)>;
男 | 小明,谢霆锋,钱小豪,周杰,郭靖,和珅,程坤,周杰伦儿,陈冠希,小月月,彭于晏,刘德华

14> 分页 l

  imit num1 num2 :显示第num1后的num2行,num2相当于步长

  当数据信息较大时,可以通过分页显示部分信息,减缓内存压力

#根据年龄从大到小排序,通过分页只显示一条信息(即显示年龄最大的用户数据)
MariaDB [testdb]> select * from students order by age desc limit ; #显示一行
+----+--------+------+--------+--------+--------+-----------+
| id | name | age | high | gender | cls_id | is_delete |
+----+--------+------+--------+--------+--------+-----------+
| | 黄蓉 | | 160.00 | 女 | | |
+----+--------+------+--------+--------+--------+-----------+
MariaDB [testdb]> select * from students order by age desc limit ;
MariaDB [testdb]> select * from students order by age desc limit ,; #显示两条数据
+----+-----------+------+--------+--------+--------+-----------+
| id | name | age | high | gender | cls_id | is_delete |
+----+-----------+------+--------+--------+--------+-----------+
| | 黄蓉 | | 160.00 | 女 | | |
| | 刘德华 | | 175.00 | 男 | | |
+----+-----------+------+--------+--------+---------+-----------+
MariaDB [testdb]> select * from students order by age desc limit ,; #显示第1行后面的3行,从2开始的3行,即234行
+----+-----------+------+--------+--------+--------+-----------+
| id | name | age | high | gender | cls_id | is_delete |
+----+-----------+------+--------+--------+--------+-----------+
| | 刘德华 | | 175.00 | 男 | | |
| | 钱小豪 | | 178.00 | 男 | | |
| | 和珅 | | 166.00 | 男 | | |
+----+-----------+------+--------+--------+--------+-----------+

2. 连接查询

  连接查询即多个表查询,包括内关联、外关联查询和自关联查询。例如创建的classes和students两个表通过classes.id和students.cls_id联系起来属于内关联查询,而外关联查询包括左关联和右关联,左右之分是按查询基础来区分的,如左关联以左边的表为基础,右关联以右边的表为基础。内外关联只是在查询时通过设定的标识来进行联系,但他们并没有什么联系,让他们真正有联系的是外键的作用,而自关联不同,比如地区的规划,省市区街道等,若每个规划阶段有一个表,各个表通过外键构成子父关系,而且各个表的各个字段都相同,因此可以通过自己关联自己建在一个表里面来实现自关联而不通过外键实现关联。自关联可以通过以下的表来了解:

id

name

f

省份

1

北京市

null

2

天津市

null

3

河北省

null

4

山西省

null

地级市

5

海淀区

1

6

滨海区

2

7

沧州市

3

8

大同市

4

9

朝阳区

1

10

武清区

2

11

石家庄

3

12

太原市

4

县级市

13

西二旗

5

14

大港

6

15

任丘市

7

16

清徐

8

17

中关村

5

18

汉沽

6

19

河间市

7

20

阳曲

8

  单独一个表通过id和f进行关联,如海淀区f=1对应北京市id=1,中关村f=5对应海淀区id=5,通过自关联构成了一条完整的子父连接,北京市--海淀区--中关村、 天津市--滨海区--大港等。

2.1  内关联查询

  1)给class表里写入数据用作实验

MariaDB [testdb]> ;create table classes(                     #创建classes表
-> id int unsigned auto_increment primary key not null,
-> name varchar() not null
-> );
MariaDB [testdb]> insert into classes values (, '云唯_01期'),(, '云唯_02期');
Query OK, rows affected (0.003 sec)
Records: Duplicates: Warnings:
MariaDB [testdb]> select * from classes;
+----+------------------+
| id | name |
+----+------------------+
| | 云唯_01期 |
| | 云唯_02期 |
+----+------------------+

  2) 连接查询(内关联查询)

#两个表连接起来取交集查询
MariaDB [testdb]> select * from classes inner join students on classes.id=students.cls_id; #内连接;首选条件语句为on
+----+--------------+----+--------------+------+--------+--------+--------+-----------+
| id | name | id | name | age | high | gender | cls_id | is_delete |
+----+--------------+----+--------------+------+--------+--------+--------+-----------+
| | 云唯_01期 | | 小明 | | 180.00 | 男 | | |
| | 云唯_02期 | | 小月月 | | 180.00 | 男 | | |
| | 云唯_01期 | | 彭于晏 | | 185.00 | 男 | | |
| | 云唯_02期 | | 刘德华 | | 175.00 | 男 | | |
| | 云唯_01期 | | 黄蓉 | | 160.00 | 女 | | |
| | 云唯_02期 | | 凤姐 | | 150.00 | 保密 | | |
| | 云唯_01期 | | 王祖贤 | | 170.00 | 女 | | |
| | 云唯_01期 | | 周杰伦儿 | | NULL | 男 | | |
| | 云唯_02期 | | 程坤 | | 181.00 | 男 | | |
| | 云唯_02期 | | 和珅 | | 166.00 | 男 | | |
| | 云唯_01期 | | 周杰 | | 178.00 | 男 | | |
| | 云唯_01期 | | 钱小豪 | | 178.00 | 男 | | |
| | 云唯_01期 | | 谢霆锋 | | 175.00 | 男 | | |
| | 云唯_01期 | | 陈冠希 | | 175.00 | 男 | | |
+----+--------------+----+--------------+------+--------+--------+--------+-----------+ #仅显示班级和姓名字段
MariaDB [testdb]> select classes.name,students.name from classes inner join students on classes.id=students.cls_id;
MariaDB [testdb]> select c.name,s.name from classes as c inner join students as s on c.id=s.cls_id; # 别名
MariaDB [testdb]> select c.name,s.name from classes as c inner join students as s on c.id=s.cls_id order by c.name; #以班级名排序
+-------------+--------------+
| name | name |
+-------------+--------------+
| 云唯_01期 | 小明 |
| 云唯_01期 | 彭于晏 |
| 云唯_01期 | 黄蓉 |
| 云唯_01期 | 王祖贤 |
| 云唯_01期 | 周杰 |
| 云唯_01期 | 谢霆锋 |
| 云唯_01期 | 周杰伦儿 |
| 云唯_01期 | 钱小豪 |
| 云唯_01期 | 陈冠希 |
| 云唯_02期 | 程坤 |
| 云唯_02期 | 小月月 |
| 云唯_02期 | 刘德华 |
| 云唯_02期 | 凤姐 |
| 云唯_02期 | 和珅 |
+-------------+--------------+

  可以看到,显示的仅是cls_id 为1、2的数据,即和classes.id一一对应的数据,并没有查询到cls_id为345的数据。这便是内关联查询的效果。

2.2  外关联查询

  1)左右关联

#继续以classes表和students表为例
#左关联:
MariaDB [testdb]> select c.name,s.name from classes as c left join students as s on c.id=s.cls_id order by c.name; #以左边的c.name为基准查询,只能查询到cls-id位为1、2的数据
+--------------+--------------+
| name | name |
+--------------+--------------+
| 云唯_01期 | 小明 |
| 云唯_01期 | 彭于晏 |
| 云唯_01期 | 黄蓉 |
| 云唯_01期 | 王祖贤 |
| 云唯_01期 | 周杰 |
| 云唯_01期 | 谢霆锋 |
| 云唯_01期 | 周杰伦儿 |
| 云唯_01期 | 钱小豪 |
| 云唯_01期 | 陈冠希 |
| 云唯_02期 | 程坤 |
| 云唯_02期 | 小月月 |
| 云唯_02期 | 刘德华 |
| 云唯_02期 | 凤姐 |
| 云唯_02期 | 和珅 |
+--------------+--------------+ #右关联:
MariaDB [testdb]> select c.name,s.name from classes as c right join students as s on c.id=s.cls_id order by c.name; #以s.name为基准,cls_id取值为12345,而c.name仅支持12,所以钱44条数据为空(null)
+--------------+-------------+
| name | name |
+--------------+-------------+
| NULL | 金星 |
| NULL | 郭靖 |
| NULL | 刘亦菲 |
| NULL | 静香 |
| 云唯_01期 | 周杰伦儿 |
| 云唯_01期 | 钱小豪 |
| 云唯_01期 | 陈冠希 |
| 云唯_01期 | 小明 |
| 云唯_01期 | 彭于晏 |
| 云唯_01期 | 黄蓉 |
| 云唯_01期 | 王祖贤 |
| 云唯_01期 | 周杰 |
| 云唯_01期 | 谢霆锋 |
| 云唯_02期 | 小月月 |
| 云唯_02期 | 刘德华 |
| 云唯_02期 | 凤姐 |
| 云唯_02期 | 和珅 |
| 云唯_02期 | 程坤 |
+--- ---------+- ------------+

2.3 自 关联查询

  1)创建areas表用作实验

MariaDB [testdb]> create table areas(
-> aid int primary key auto_increment,
-> name varchar(),
-> pid int
-> );
MariaDB [testdb]> show tables;
+--------------------+
| Tables_in_testdb |
+--------------------+
| areas |
| classes |
| students | --写入数据
--省份
insert into areas(aid,name,pid) values(,'北京市',null);
insert into areas(aid,name,pid) values(,'天津市',null);
insert into areas(aid,name,pid) values(,'河北省',null);
insert into areas(aid,name,pid) values(,'山西省',null); --地级市
insert into areas(aid,name,pid) values(,'海淀区',);
insert into areas(aid,name,pid) values(,'滨海区',);
insert into areas(aid,name,pid) values(,'沧州市',);
insert into areas(aid,name,pid) values(,'大同市',);
insert into areas(aid,name,pid) values(,'朝阳区',);
insert into areas(aid,name,pid) values(,'武清区',);
insert into areas(aid,name,pid) values(,'石家庄',);
insert into areas(aid,name,pid) values(,'太原市',); --县级市
insert into areas(aid,name,pid) values(,'西二旗',);
insert into areas(aid,name,pid) values(,'大港',);
insert into areas(aid,name,pid) values(,'任丘市',);
insert into areas(aid,name,pid) values(,'清徐',);
insert into areas(aid,name,pid) values(,'中关村',);
insert into areas(aid,name,pid) values(,'汉沽',);
insert into areas(aid,name,pid) values(,'河间市',);
insert into areas(aid,name,pid) values(,'阳曲',);
MariaDB [testdb]> select * from areas;
+-----+-----------+------+
| aid | name | pid |
+-----+-----------+------+
| | 北京市 | NULL |
| | 天津市 | NULL |
| | 河北省 | NULL |
| | 山西省 | NULL |
| | 海淀区 | |
| | 滨海区 | |
| | 沧州市 | |
| | 大同市 | |
| | 朝阳区 | |
| | 武清区 | |
| | 石家庄 | |
| | 太原市 | |
| | 西二旗 | |
| | 大港 | |
| | 任丘市 | |
| | 清徐 | |
| | 中关村 | |
| | 汉沽 | |
| | 河间市 | |
| | 阳曲 | |
+-----+----------+------+

  2)自关联查询

--查询北京市的所有区:
MariaDB [testdb]> select * from areas as p inner join areas as q on p.aid=q.pid where p.name='北京市'; #起别名,自关联
+-----+-----------+------+-----+-----------+------+
| aid | name | pid | aid | name | pid |
+-----+-----------+------+-----+-----------+------+
| | 北京市 | NULL | | 海淀区 | |
| | 北京市 | NULL | | 朝阳区 | |
+-----+-----------+------+-----+-----------+------+
rows in set (0.000 sec) --查询山西省的所有区:
MariaDB [testdb]> select * from areas as p inner join areas as q on p.aid=q.pid where p.name='山西省';
+-----+-----------+------+-----+-----------+------+
| aid | name | pid | aid | name | pid |
+-----+-----------+------+-----+-----------+------+
| | 山西省 | NULL | |大同市 | |
| | 山西省 | NULL | |太原市 | |
+-----+-----------+------+-----+-----------+------+

  3)子查询(嵌套查询)

#查询北京市所有的区
MariaDB [testdb]> select * from areas where pid=(select aid from areas where name='北京市');
+-----+-----------+-------+
| aid | name | pid |
+-----+-----------+-------+
| | 海淀区 | |
| | 朝阳区 | |
+-----+-----------+-------+
rows in set (0.001 sec)

MariaDB数据库(三)的更多相关文章

  1. Linux编译安装Mariadb数据库

    一.安装cmake cd /usr/local/src tar zxvf cmake-2.8.12.1.tar.gz cd cmake-2.8.12.1 ./configure 注意报错需要安装gcc ...

  2. MySQL/MariaDB数据库忘掉密码解决办法--技术流ken

    前言 有些时候我们常常会忘掉一些服务的密码,比如系统密码,我们可以进入救援模式进行修改密码,可参考我之前的博客<Centos7破解密码的两种方法--技术流ken>.但有些时候我们也会忘掉数 ...

  3. centos7.5下yum 安装mariadb数据库

    前言 mariadb 和mysql就像亲兄弟的关系,各种语法.驱动啥的,在mysql上能上的,在mariadb上基本都可以直接使用.更多的细节在此不多说. 1.删除旧版本 centos7下默认安装有m ...

  4. fedora中使用 mariadb数据库建库和建表-- mariadb数据库服务无法启动?

    /proc(进程, 过程等含义) 文件系统是一个虚拟文件系统,通过它可以使用一种新的方法在 Linux® 内核空间(内核)和用户空间(用户)之间进行通信.在 /proc 文件系统中,我们可以将对虚拟文 ...

  5. MariaDB数据库主从复制实现步骤

    一.MariaDB简介 MariaDB数据库的主从复制方案,是其自带的功能,并且主从复制并不是复制磁盘上的数据库文件,而是通过binlog日志复制到需要同步的从服务器上. MariaDB数据库支持单向 ...

  6. MariaDB数据库(一)

    1.数据库简介 1.1 什么是数据库? 简单的说,数据库就是一个存放数据的仓库,这个仓库是按照一定的数据结构(数据结构是指数据的组织形式或数据之间的联系)来组织,存储的,我们可以通过数据库提供的多种方 ...

  7. MySQL/MariaDB数据库的性能测试

      MySQL/MariaDB数据库的性能测试 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.数据库服务衡量指标 qps: query per second(每秒支持多少查询 ...

  8. MySQL/MariaDB数据库的Galera高可用性集群实战

      MySQL/MariaDB数据库的Galera高可用性集群实战 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Galera Cluster概述 1>.什么是Gale ...

  9. MySQL/MariaDB数据库的MHA实现高可用实战

      MySQL/MariaDB数据库的MHA实现高可用实战 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MySQL高可用常见的解决方案 1>.Multi-Master ...

  10. MySQL/MariaDB数据库的PROXY实现读写分离

    MySQL/MariaDB数据库的PROXY实现读写分离 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.ProxySQL概述 1>.各家互联网公司读写分离的解决方案 m ...

随机推荐

  1. [题解](树的计数)luogu_P4430猴子打架_/_luogu_P4981父子

    来源:题解 比较不错的博客:http://www.cnblogs.com/dirge/p/5503289.html 最后生成一颗无根树,有n^(n-2)种情况,打架的顺序有(n-1)!种 #inclu ...

  2. [題解](水/并查集)luogu_P2170選學霸

    很簡單的水題,因為智障沒有A所以發篇博客 同樣的題:luogu_P1455 搭配購買 用并查集維護一下所有實力相等的人的size,然而你可以選多個size......,於是跑個背包就行了,只要注意一下 ...

  3. ZROI #364. 【2018普转提day18专题】嘤嘤嘤

    ZROI #364. [2018普转提day18专题]嘤嘤嘤 直接贴代码 具体见注释 #include<stdio.h> #include<cstring> #include& ...

  4. Zip-line Codeforces - 650D || 风筝

    https://codeforces.com/contest/650/problem/D 原题? http://210.33.19.103/contest/1024/problem/2 4s 520M ...

  5. SSH之小问题解惑

    (注:以下版本指的是spring3+hibernate3+struts2) 1,web开发中,servlet对象是否线程安全? 当一个http请求到来时,web容器的调度线程(Dispach Thre ...

  6. python入门之数据类型之列表、元组、字典

    list 格式: test_list = ["a",123,[1,"b"]] 索引: >>>print(test_list[0]) " ...

  7. 深入理解synchronized

    上一篇博客虽然题目叫内置锁的基本使用,但其实也是讲synchronized关键字的使用的.这篇博客是在看了许多大佬的博客记录后总结出的synchronized更底层的知识和原理. 一.synchron ...

  8. 关于yii2自带验证码功能不显示问题

    1,验证码不显示: 首先保证你的controler 里面的captcha方法是可访问的,被分配的权限的,这个在rule里面设置. 第二,保证你的PHP GD插件已经被启用, 第三如果这样还是不显示,那 ...

  9. cocos-js一些问题

    1. setTexture和setSpriteFrame方法 修改精灵的里面的图片的时候如果是图片使用 var imgPath = "#" + this.imgName[idx] ...

  10. AmazeUI 保存浏览器数据 永久性

    //保存永久缓存数据function SaveAmuiStore(ItemName, ItemData){ if (window.localStorage) { var store = $.AMUI. ...