MySQL-Join

2024-09-04 21:48
文章标签 mysql join database

本文主要是介绍MySQL-Join,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

环境准备

三个table:书籍信息book、作者信息author、出版社信息publish。

create database temp;
use temp;
create table book (id int not null auto_increment, name varchar(20) not null, author int not null, pub int not null, price int default 0, primary key (id));
create table author (id int not null auto_increment, name varchar(20) not null, age int, primary key (id));
create table publish (id int not null auto_increment, name varchar(20) not null, city varchar(20), primary key (id));

增加测试数据:

insert into author (name, age) values ("Tom", 20);
insert into author (name, age) values ("Jerry", 25);insert into publish (name, city) values ("PUB Tigger", "BJ");
insert into publish (name, city) values ("PUB Lion", "SH");
insert into publish (name, city) values ("PUB Rabbit", "SZ");insert into book (name, author, pub, price) values ("Good Mood", 1, 1, 110);
insert into book (name, author, pub, price) values ("Rainy Day", 2, 3, 230);
insert into book (name, author, pub, price) values ("On the Road", 2, 2, 220);

验证:

MariaDB [temp]> select * from book;
+----+-------------+--------+-----+-------+
| id | name        | author | pub | price |
+----+-------------+--------+-----+-------+
|  1 | Good Mood   |      1 |   1 |   110 |
|  2 | Rainy Day   |      2 |   3 |   230 |
|  3 | On the Road |      2 |   2 |   220 |
+----+-------------+--------+-----+-------+
3 rows in set (0.00 sec)MariaDB [temp]> select * from author;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | Tom   |   20 |
|  2 | Jerry |   25 |
+----+-------+------+
2 rows in set (0.00 sec)MariaDB [temp]> select * from publish;
+----+------------+------+
| id | name       | city |
+----+------------+------+
|  1 | PUB Tigger | BJ   |
|  2 | PUB Lion   | SH   |
|  3 | PUB Rabbit | SZ   |
+----+------------+------+
3 rows in set (0.00 sec)

Join

根据三个表,查询完整的信息;Join有两种使用方式。

MariaDB [temp]> select book.name, author.name as author, publish.name as publish, publish.city, book.price from book, author, publish where book.author=author.id and book.pub=publish.id;
+-------------+--------+------------+------+-------+
| name        | author | publish    | city | price |
+-------------+--------+------------+------+-------+
| Good Mood   | Tom    | PUB Tigger | BJ   |   110 |
| Rainy Day   | Jerry  | PUB Rabbit | SZ   |   230 |
| On the Road | Jerry  | PUB Lion   | SH   |   220 |
+-------------+--------+------------+------+-------+
3 rows in set (0.00 sec)MariaDB [temp]> select book.name, author.name as author, publish.name as publish, publish.city, book.price from book inner join author inner join publish on book.author=author.id and book.pub=publish.id;
+-------------+--------+------------+------+-------+
| name        | author | publish    | city | price |
+-------------+--------+------------+------+-------+
| Good Mood   | Tom    | PUB Tigger | BJ   |   110 |
| Rainy Day   | Jerry  | PUB Rabbit | SZ   |   230 |
| On the Road | Jerry  | PUB Lion   | SH   |   220 |
+-------------+--------+------------+------+-------+
3 rows in set (0.00 sec)MariaDB [temp]> 

同一个table被查询两次

对上面的场景进行增强,假设一本书有两个作者。

新建一个表book_en。

create table book_en (id int not null auto_increment, name varchar(20) not null, author1 int not null, author2 int not null, pub int not null, price int default 0, primary key (id));insert into book_en (name, author1, author2, pub, price) values ("A Bad Habit", 1, 2, 1, 110);
insert into book_en (name, author1, author2, pub, price) values ("Keep Healthy", 2, 1, 3, 230);
insert into book_en (name, author1, author2, pub, price) values ("Toast", 1, 2, 2, 220);

查询:

MariaDB [temp]> select * from book_en;
+----+--------------+---------+---------+-----+-------+
| id | name         | author1 | author2 | pub | price |
+----+--------------+---------+---------+-----+-------+
|  4 | A Bad Habit  |       1 |       2 |   1 |   110 |
|  5 | Keep Healthy |       2 |       1 |   3 |   230 |
|  6 | Toast        |       1 |       2 |   2 |   220 |
+----+--------------+---------+---------+-----+-------+
3 rows in set (0.00 sec)MariaDB [temp]> select * from author;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | Tom   |   20 |
|  2 | Jerry |   25 |
+----+-------+------+
2 rows in set (0.00 sec)MariaDB [temp]> select book_en.name as name, a1.name as author1, a2.name as author2, publish.name as publish, publish.city as city, book_en.price from book_en, author a1, author a2, publish where book_en.author1=a1.id and book_en.author2=a2.id and book_en.pub=publish.id;
+--------------+---------+---------+------------+------+-------+
| name         | author1 | author2 | publish    | city | price |
+--------------+---------+---------+------------+------+-------+
| Keep Healthy | Jerry   | Tom     | PUB Rabbit | SZ   |   230 |
| A Bad Habit  | Tom     | Jerry   | PUB Tigger | BJ   |   110 |
| Toast        | Tom     | Jerry   | PUB Lion   | SH   |   220 |
+--------------+---------+---------+------------+------+-------+
3 rows in set (0.00 sec)MariaDB [temp]> select book_en.name as name, a1.name as author1, a2.name as author2, publish.name as publish, publish.city as city, book_en.price from book_en inner join author a1 inner join author a2 inner join publish where book_en.author1=a1.id and book_en.author2=a2.id and book_en.pub=publish.id;
+--------------+---------+---------+------------+------+-------+
| name         | author1 | author2 | publish    | city | price |
+--------------+---------+---------+------------+------+-------+
| Keep Healthy | Jerry   | Tom     | PUB Rabbit | SZ   |   230 |
| A Bad Habit  | Tom     | Jerry   | PUB Tigger | BJ   |   110 |
| Toast        | Tom     | Jerry   | PUB Lion   | SH   |   220 |
+--------------+---------+---------+------------+------+-------+
3 rows in set (0.00 sec)MariaDB [temp]> 

上面的where可以改成on。

另外一个例子

城市city,两个城市之间的距离distance。

建表&准备测试数据

create table city (id int not null auto_increment, name varchar(20), primary key (id));
create table distance (id int not null auto_increment, from_id int not null, to_id int not null, dist int not null, primary key (id));insert into city (name) values("BJ");
insert into city (name) values("SH");
insert into city (name) values("SZ");insert into distance (from_id, to_id, dist) values (1, 2, 1200);
insert into distance (from_id, to_id, dist) values (2, 3, 2300);
insert into distance (from_id, to_id, dist) values (3, 1, 3100);

数据验证

MariaDB [temp]> select * from distance;
+----+---------+-------+------+
| id | from_id | to_id | dist |
+----+---------+-------+------+
|  1 |       1 |     2 | 1200 |
|  2 |       2 |     3 | 2300 |
|  3 |       3 |     1 | 3100 |
+----+---------+-------+------+
3 rows in set (0.00 sec)MariaDB [temp]> select * from city;
+----+------+
| id | name |
+----+------+
|  1 | BJ   |
|  2 | SH   |
|  3 | SZ   |
+----+------+
3 rows in set (0.00 sec)

Join

MariaDB [temp]> select c1.name as city1, c2.name as city2, distance.dist dist from distance inner join city c1 inner join city c2 where distance.from_id=c1.id and distance.to_id=c2.id;
+-------+-------+------+
| city1 | city2 | dist |
+-------+-------+------+
| BJ    | SH    | 1200 |
| SH    | SZ    | 2300 |
| SZ    | BJ    | 3100 |
+-------+-------+------+
3 rows in set (0.01 sec)MariaDB [temp]> select c1.name as city1, c2.name as city2, distance.dist dist from city c1 inner join city c2 inner join distance on distance.from_id=c1.id and distance.to_id=c2.id;
+-------+-------+------+
| city1 | city2 | dist |
+-------+-------+------+
| BJ    | SH    | 1200 |
| SH    | SZ    | 2300 |
| SZ    | BJ    | 3100 |
+-------+-------+------+
3 rows in set (0.00 sec)MariaDB [temp]> 

这篇关于MySQL-Join的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1137126

相关文章

MySQL字符串转数值的方法全解析

《MySQL字符串转数值的方法全解析》在MySQL开发中,字符串与数值的转换是高频操作,本文从隐式转换原理、显式转换方法、典型场景案例、风险防控四个维度系统梳理,助您精准掌握这一核心技能,需要的朋友可... 目录一、隐式转换:自动但需警惕的&ld编程quo;双刃剑”二、显式转换:三大核心方法详解三、典型场景

MySQL中between and的基本用法、范围查询示例详解

《MySQL中betweenand的基本用法、范围查询示例详解》BETWEENAND操作符在MySQL中用于选择在两个值之间的数据,包括边界值,它支持数值和日期类型,示例展示了如何使用BETWEEN... 目录一、between and语法二、使用示例2.1、betwphpeen and数值查询2.2、be

MySQL快速复制一张表的四种核心方法(包括表结构和数据)

《MySQL快速复制一张表的四种核心方法(包括表结构和数据)》本文详细介绍了四种复制MySQL表(结构+数据)的方法,并对每种方法进行了对比分析,适用于不同场景和数据量的复制需求,特别是针对超大表(1... 目录一、mysql 复制表(结构+数据)的 4 种核心方法(面试结构化回答)方法 1:CREATE

SQL Server中行转列方法详细讲解

《SQLServer中行转列方法详细讲解》SQL行转列、列转行可以帮助我们更方便地处理数据,生成需要的报表和结果集,:本文主要介绍SQLServer中行转列方法的相关资料,需要的朋友可以参考下... 目录前言一、为什么需要行转列二、行转列的基本概念三、使用PIVOT运算符进行行转列1.创建示例数据表并插入数

MySQL MHA集群详解(数据库高可用)

《MySQLMHA集群详解(数据库高可用)》MHA(MasterHighAvailability)是开源MySQL高可用管理工具,用于自动故障检测与转移,支持异步或半同步复制的MySQL主从架构,本... 目录mysql 高可用方案:MHA 详解与实战1. MHA 简介2. MHA 的组件组成(1)MHA

SQL 注入攻击(SQL Injection)原理、利用方式与防御策略深度解析

《SQL注入攻击(SQLInjection)原理、利用方式与防御策略深度解析》本文将从SQL注入的基本原理、攻击方式、常见利用手法,到企业级防御方案进行全面讲解,以帮助开发者和安全人员更系统地理解... 目录一、前言二、SQL 注入攻击的基本概念三、SQL 注入常见类型分析1. 基于错误回显的注入(Erro

MySQL基本表查询操作汇总之单表查询+多表操作大全

《MySQL基本表查询操作汇总之单表查询+多表操作大全》本文全面介绍了MySQL单表查询与多表操作的关键技术,包括基本语法、高级查询、表别名使用、多表连接及子查询等,并提供了丰富的实例,感兴趣的朋友跟... 目录一、单表查询整合(一)通用模版展示(二)举例说明(三)注意事项(四)Mapper简单举例简单查询

MySQL中的DELETE删除数据及注意事项

《MySQL中的DELETE删除数据及注意事项》MySQL的DELETE语句是数据库操作中不可或缺的一部分,通过合理使用索引、批量删除、避免全表删除、使用TRUNCATE、使用ORDERBY和LIMI... 目录1. 基本语法单表删除2. 高级用法使用子查询删除删除多表3. 性能优化策略使用索引批量删除避免

MySQL 数据库进阶之SQL 数据操作与子查询操作大全

《MySQL数据库进阶之SQL数据操作与子查询操作大全》本文详细介绍了SQL中的子查询、数据添加(INSERT)、数据修改(UPDATE)和数据删除(DELETE、TRUNCATE、DROP)操作... 目录一、子查询:嵌套在查询中的查询1.1 子查询的基本语法1.2 子查询的实战示例二、数据添加:INSE

在SpringBoot+MyBatis项目中实现MySQL读写分离的实战指南

《在SpringBoot+MyBatis项目中实现MySQL读写分离的实战指南》在SpringBoot和MyBatis项目中实现MySQL读写分离,主要有两种思路:一种是在应用层通过代码和配置手动控制... 目录如何选择实现方案核心实现:应用层手动分离实施中的关键问题与解决方案总结在Spring Boot和