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的not exists走不走索引

《浅谈mysql的notexists走不走索引》在MySQL中,​NOTEXISTS子句是否使用索引取决于子查询中关联字段是否建立了合适的索引,下面就来介绍一下mysql的notexists走不走索... 在mysql中,​NOT EXISTS子句是否使用索引取决于子查询中关联字段是否建立了合适的索引。以下

Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式

《Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式》本文详细介绍如何使用Java通过JDBC连接MySQL数据库,包括下载驱动、配置Eclipse环境、检测数据库连接等关键步骤,... 目录一、下载驱动包二、放jar包三、检测数据库连接JavaJava 如何使用 JDBC 连接 mys

SQL中如何添加数据(常见方法及示例)

《SQL中如何添加数据(常见方法及示例)》SQL全称为StructuredQueryLanguage,是一种用于管理关系数据库的标准编程语言,下面给大家介绍SQL中如何添加数据,感兴趣的朋友一起看看吧... 目录在mysql中,有多种方法可以添加数据。以下是一些常见的方法及其示例。1. 使用INSERT I

Qt使用QSqlDatabase连接MySQL实现增删改查功能

《Qt使用QSqlDatabase连接MySQL实现增删改查功能》这篇文章主要为大家详细介绍了Qt如何使用QSqlDatabase连接MySQL实现增删改查功能,文中的示例代码讲解详细,感兴趣的小伙伴... 目录一、创建数据表二、连接mysql数据库三、封装成一个完整的轻量级 ORM 风格类3.1 表结构

MySQL 中的 CAST 函数详解及常见用法

《MySQL中的CAST函数详解及常见用法》CAST函数是MySQL中用于数据类型转换的重要函数,它允许你将一个值从一种数据类型转换为另一种数据类型,本文给大家介绍MySQL中的CAST... 目录mysql 中的 CAST 函数详解一、基本语法二、支持的数据类型三、常见用法示例1. 字符串转数字2. 数字

Mysql实现范围分区表(新增、删除、重组、查看)

《Mysql实现范围分区表(新增、删除、重组、查看)》MySQL分区表的四种类型(范围、哈希、列表、键值),主要介绍了范围分区的创建、查询、添加、删除及重组织操作,具有一定的参考价值,感兴趣的可以了解... 目录一、mysql分区表分类二、范围分区(Range Partitioning1、新建分区表:2、分

MySQL 定时新增分区的实现示例

《MySQL定时新增分区的实现示例》本文主要介绍了通过存储过程和定时任务实现MySQL分区的自动创建,解决大数据量下手动维护的繁琐问题,具有一定的参考价值,感兴趣的可以了解一下... mysql创建好分区之后,有时候会需要自动创建分区。比如,一些表数据量非常大,有些数据是热点数据,按照日期分区MululbU

SQL Server配置管理器无法打开的四种解决方法

《SQLServer配置管理器无法打开的四种解决方法》本文总结了SQLServer配置管理器无法打开的四种解决方法,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录方法一:桌面图标进入方法二:运行窗口进入检查版本号对照表php方法三:查找文件路径方法四:检查 S

MySQL 删除数据详解(最新整理)

《MySQL删除数据详解(最新整理)》:本文主要介绍MySQL删除数据的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、前言二、mysql 中的三种删除方式1.DELETE语句✅ 基本语法: 示例:2.TRUNCATE语句✅ 基本语

MySQL中查找重复值的实现

《MySQL中查找重复值的实现》查找重复值是一项常见需求,比如在数据清理、数据分析、数据质量检查等场景下,我们常常需要找出表中某列或多列的重复值,具有一定的参考价值,感兴趣的可以了解一下... 目录技术背景实现步骤方法一:使用GROUP BY和HAVING子句方法二:仅返回重复值方法三:返回完整记录方法四: