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出现ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost‘ (10061)的解决方法

《mysql出现ERROR2003(HY000):Can‘tconnecttoMySQLserveron‘localhost‘(10061)的解决方法》本文主要介绍了mysql出现... 目录前言:第一步:第二步:第三步:总结:前言:当你想通过命令窗口想打开mysql时候发现提http://www.cpp

MySQL大表数据的分区与分库分表的实现

《MySQL大表数据的分区与分库分表的实现》数据库的分区和分库分表是两种常用的技术方案,本文主要介绍了MySQL大表数据的分区与分库分表的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有... 目录1. mysql大表数据的分区1.1 什么是分区?1.2 分区的类型1.3 分区的优点1.4 分

MySQL错误代码2058和2059的解决办法

《MySQL错误代码2058和2059的解决办法》:本文主要介绍MySQL错误代码2058和2059的解决办法,2058和2059的错误码核心都是你用的客户端工具和mysql版本的密码插件不匹配,... 目录1. 前置理解2.报错现象3.解决办法(敲重点!!!)1. php前置理解2058和2059的错误

Mysql删除几亿条数据表中的部分数据的方法实现

《Mysql删除几亿条数据表中的部分数据的方法实现》在MySQL中删除一个大表中的数据时,需要特别注意操作的性能和对系统的影响,本文主要介绍了Mysql删除几亿条数据表中的部分数据的方法实现,具有一定... 目录1、需求2、方案1. 使用 DELETE 语句分批删除2. 使用 INPLACE ALTER T

MySQL INSERT语句实现当记录不存在时插入的几种方法

《MySQLINSERT语句实现当记录不存在时插入的几种方法》MySQL的INSERT语句是用于向数据库表中插入新记录的关键命令,下面:本文主要介绍MySQLINSERT语句实现当记录不存在时... 目录使用 INSERT IGNORE使用 ON DUPLICATE KEY UPDATE使用 REPLACE

MySQL Workbench 安装教程(保姆级)

《MySQLWorkbench安装教程(保姆级)》MySQLWorkbench是一款强大的数据库设计和管理工具,本文主要介绍了MySQLWorkbench安装教程,文中通过图文介绍的非常详细,对大... 目录前言:详细步骤:一、检查安装的数据库版本二、在官网下载对应的mysql Workbench版本,要是

mysql数据库重置表主键id的实现

《mysql数据库重置表主键id的实现》在我们的开发过程中,难免在做测试的时候会生成一些杂乱无章的SQL主键数据,本文主要介绍了mysql数据库重置表主键id的实现,具有一定的参考价值,感兴趣的可以了... 目录关键语法演示案例在我们的开发过程中,难免在做测试的时候会生成一些杂乱无章的SQL主键数据,当我们

浅谈mysql的sql_mode可能会限制你的查询

《浅谈mysql的sql_mode可能会限制你的查询》本文主要介绍了浅谈mysql的sql_mode可能会限制你的查询,这个问题主要说明的是,我们写的sql查询语句违背了聚合函数groupby的规则... 目录场景:问题描述原因分析:解决方案:第一种:修改后,只有当前生效,若是mysql服务重启,就会失效;

MySQL多列IN查询的实现

《MySQL多列IN查询的实现》多列IN查询是一种强大的筛选工具,它允许通过多字段组合快速过滤数据,本文主要介绍了MySQL多列IN查询的实现,具有一定的参考价值,感兴趣的可以了解一下... 目录一、基础语法:多列 IN 的两种写法1. 直接值列表2. 子查询二、对比传统 OR 的写法三、性能分析与优化1.

MySQL新增字段后Java实体未更新的潜在问题与解决方案

《MySQL新增字段后Java实体未更新的潜在问题与解决方案》在Java+MySQL的开发中,我们通常使用ORM框架来映射数据库表与Java对象,但有时候,数据库表结构变更(如新增字段)后,开发人员可... 目录引言1. 问题背景:数据库与 Java 实体不同步1.1 常见场景1.2 示例代码2. 不同操作