mysql的inner join, left join, right join

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: mysql的inner join, left join, right join 0. 先看表数据:mysql> select * from a;idage1102203304404 rows in set (0.

mysql的inner join, left join, right join
 0. 先看表数据:

mysql> select * from a;
id age
1 10
2 20
3 30
4 40

4 rows in set (0.00 sec)

mysql> select * from b;
id score
1 100
2 200
3 300
5 500

4 rows in set (0.00 sec)
 

     1. inner join最简单,我们之前说过, 来看下:

mysql> select * from a inner join b;
id age id score
1 10 1 100
2 20 1 100
3 30 1 100
4 40 1 100
1 10 2 200
2 20 2 200
3 30 2 200
4 40 2 200
1 10 3 300
2 20 3 300
3 30 3 300
4 40 3 300
1 10 5 500
2 20 5 500
3 30 5 500
4 40 5 500

16 rows in set (0.00 sec)
    可以用on/where过滤下:

mysql> select * from a inner join b on a.id = b.id;
id age id score
1 10 1 100
2 20 2 200
3 30 3 300

3 rows in set (0.00 sec)

mysql> select * from a inner join b where a.id = b.id;
id age id score
1 10 1 100
2 20 2 200
3 30 3 300

3 rows in set (0.00 sec)
      之所以可以用where, 是因为where之前本身就有结果。

 

     2. 再看left join:

mysql> select * from a left join b;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
     所以很显然不能用where:

mysql> select * from a left join b where a.id = b.id;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where a.id = b.id' at line 1
      只能用on:

mysql> select * from a left join b on a.id = b.id;
id age id score
1 10 1 100
2 20 2 200
3 30 3 300
4 40 NULL NULL

4 rows in set (0.00 sec)
 

     3. right join和left join类似,来看看right join的结果:

mysql> select * from a right join;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
mysql> select * from a right join where a.id = b.id;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where a.id = b.id' at line 1

mysql> select * from a right join b on a.id = b.id;
id age id score
1 10 1 100
2 20 2 200
3 30 3 300
NULL NULL 5 500

4 rows in set (0.00 sec)
 

 

    一目了然,不必多说。

作者:stpeace
来源:CSDN
原文:https://blog.csdn.net/stpeace/article/details/91507571
版权声明:本文为博主原创文章,转载请附上博文链接!

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
4月前
|
关系型数据库 MySQL
mysql join 实践
mysql join 实践
21 0
|
6月前
|
SQL 算法 关系型数据库
深入理解MySQL中的Join算法
在数据库处理中,Join操作是最基本且最重要的操作之一,它能将不同的表连接起来,实现对数据集的更深层次分析。
331 8
深入理解MySQL中的Join算法
|
4月前
|
关系型数据库 MySQL
MySQL left join 查询 多条数据
MySQL left join 查询 多条数据
43 0
|
1月前
|
存储 算法 关系型数据库
MySQL的JOIN到底是怎么玩的
在MySQL中,查询操作通常会涉及到联结不同表格,而JOIN命令则在这一过程中扮演了关键角色。在JOIN操作中,我们通常会使用三种不同的方式,分别是内连接、左连接以及右连接。
|
4月前
|
关系型数据库 MySQL 数据库
MySQL 数据库操作指南:LIMIT,OFFSET 和 JOIN 的使用
您可以通过使用"LIMIT"语句来限制查询返回的记录数量。以下是一个示例,获取您自己的Python服务器中"customers"表中的前5条记录:
94 1
|
5月前
|
关系型数据库 MySQL 索引
mysql join
mysql join
38 0
|
6月前
|
关系型数据库 MySQL 数据库
【MySQL用法】Mysql字符串截取总结:left()函数、right()函数、substring()函数、substring_index()函数
【MySQL用法】Mysql字符串截取总结:left()函数、right()函数、substring()函数、substring_index()函数
48 0
|
14天前
|
关系型数据库 MySQL 数据库
mysql卸载、下载、安装(window版本)
mysql卸载、下载、安装(window版本)
|
1月前
|
关系型数据库 MySQL 数据库
rds安装数据库客户端工具
安装阿里云RDS的数据库客户端涉及在本地安装对应类型(如MySQL、PostgreSQL)的客户端工具。对于MySQL,可选择MySQL Command-Line Client或图形化工具如Navicat,安装后输入RDS实例的连接参数进行连接。对于PostgreSQL,可以使用`psql`命令行工具或图形化客户端如PgAdmin。首先从阿里云控制台获取连接信息,然后按照官方文档安装客户端,最后配置客户端连接以确保遵循安全指引。
82 1
|
3天前
|
关系型数据库 MySQL 数据库
《MySQL 简易速速上手小册》第1章:MySQL 基础和安装(2024 最新版)
《MySQL 简易速速上手小册》第1章:MySQL 基础和安装(2024 最新版)
25 4