【转】mysql数据库中实现内连接、左连接、右连接

内连接:把两个表中数据对应的数据查出来 
外连接:以某个表为基础把对应数据查出来

首先创建数据库中的表,数据库代码如下:

/*
Navicat MySQL Data Transfer
Source Server : localhost_3306
Source Server Version : 50150
Source Host : localhost:3306
Source Database : store
Target Server Type : MYSQL
Target Server Version : 50150
File Encoding : 65001
Date: 2010-12-15 16:27:53
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `grade`
-- ----------------------------
DROP TABLE IF EXISTS `grade`;
CREATE TABLE `grade` (
`no` int(11) NOT NULL AUTO_INCREMENT,
`grade` int(11) NOT NULL,
PRIMARY KEY (`no`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of grade
-- ----------------------------
INSERT INTO grade VALUES ('', '');
INSERT INTO grade VALUES ('', '');
INSERT INTO grade VALUES ('', '');
-- ----------------------------
-- Table structure for `student`
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`no` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
PRIMARY KEY (`no`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO student VALUES ('', 'a');
INSERT INTO student VALUES ('', 'b');
INSERT INTO student VALUES ('', 'c');
INSERT INTO student VALUES ('', 'd');

student表中的字段分别是no和name,grade表中的字段是no和grade。两张表中的no都代表的是学生的学号。

查询student表的结果:

mysql> select * from grade;
+----+-------+
| no | grade |
+----+-------+
| 1 | 90 |
| 2 | 80 |
| 3 | 70 |
+----+-------+
3 rows in set

查询grade表的结果:

mysql> select * from student;
+----+------+
| no | name |
+----+------+
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | d |
+----+------+
4 rows in set

内连接 inner join(查找条件中对应的数据,no4没有数据不列出来)

mysql> select * from student s inner join grade g on s.no=g.no;

+----+------+----+-------+
| no | name | no | grade |
+----+------+----+-------+
| 1 | a | 1 | 90 |
| 2 | b | 2 | 80 |
| 3 | c | 3 | 70 |
+----+------+----+-------+
3 rows in set

左连接(左表中所有数据,右表中对应数据)

mysql> select * from student as s left join grade as
g on s.no=g.no;
+----+------+------+-------+
| no | name | no | grade |
+----+------+------+-------+
| 1 | a | 1 | 90 |
| 2 | b | 2 | 80 |
| 3 | c | 3 | 70 |
| 4 | d | NULL | NULL |
+----+------+------+-------+
4 rows in set

右连接(右表中所有数据,左表中对应数据)

mysql> select * from student as s right
join grade as g on s.no=g.no;
+----+------+----+-------+
| no | name | no | grade |
+----+------+----+-------+
| 1 | a | 1 | 90 |
| 2 | b | 2 | 80 |
| 3 | c | 3 | 70 |
+----+------+----+-------+
3 rows in set

【转】mysql数据库中实现内连接、左连接、右连接的更多相关文章

  1. mysql数据库中实现内连接、左连接、右连接

    原文:http://www.cnblogs.com/xwdreamer/archive/2010/12/15/2297058.html 内连接:把两个表中数据对应的数据查出来 外连接:以某个表为基础把 ...

  2. mysql数据库中的多表查询(内连接,外连接,子查询)

    用两个表(a_table.b_table),关联字段a_table.a_id和b_table.b_id来演示一下MySQL的内连接.外连接( 左(外)连接.右(外)连接.全(外)连接). MySQL版 ...

  3. 通过JSP网页连接MySQL数据库,从MySQL数据库中读出一张表并显示在JSP网页中

    1.安装所需软件 ①安装java和tomcat,建立JSP网页最基础的软件②安装MySQL数据库(下载地址:https://www.mysql.com/)③安装Navicat Premium来查看数据 ...

  4. Mysql一个非常有用的内置函数今天碰到要把MySQL数据库中的varchar转换成date类型进

    Mysql一个非常有用的内置函数 今天碰到要把MySQL数据库中的varchar转换成date类型进行时间的比较和查询.在网上找了找,发现MySQL也跟其他数据库一样有自己内置的转换函数:str_to ...

  5. Linux系统下 MYSQL数据库中的数据库文件在本机内迁移 (需暂停服务的方式)

    Linux系统下 MYSQL数据库中的数据库文件在本机内迁移 本机采用Ubuntu16.04系统,tar方式安装MySQL5.7.21 数据库安装文件夹为    /home/devil/mysql 现 ...

  6. PHP往mysql数据库中写入中文失败

    该类问题解决办法就是 在建立数据库连接之后,将该连接的编码方式改为中文. 代码如下: $linkID=@mysql_connect("localhost","root&q ...

  7. MySQL存储引擎的实际应用以及对MySQL数据库中各主要存储引擎的独特特点的描述

    MySQL存储引擎的实际应用以及对MySQL数据库中各主要存储引擎的独特特点的描述: 1.MySQL有多种存储引擎: MyISAM.InnoDB.MERGE.MEMORY(HEAP).BDB(Berk ...

  8. C#实现MySQL数据库中的blob数据存储

    在MySQL数据库中,有一种blob数据类型,用来存储文件.C#编程语言操作MySQL数据库需要使用MySQL官方组件MySQL.Data.dll. Mysql.Data.dll(6.9.6)组件下载 ...

  9. 用JDBC把Excel中的数据导入到Mysql数据库中

    步骤:0.在Mysql数据库中先建好table 1.从Excel表格读数据 2.用JDBC连接Mysql数据库 3.把读出的数据导入到Mysql数据库的相应表中 其中,步骤0的table我是先在Mys ...

随机推荐

  1. loadView与viewDidLoad不同 && loadView学习总结

    loadView学习总结 UIViewController类或其子类会在初始化时创建一个UIView对象,会作为控制器的默认视图显示出来,可以通过self.view寻址访问.但没有调用loadView ...

  2. Unity图片处理类,包括压缩、截屏和滤镜

    先上代码: 1 using System.Threading; using UnityEngine; using System.IO; using System.Collections; public ...

  3. 工作总结:将电脑中的ARP缓存清空黑屏命令

    ARP -d 将电脑中的ARP缓存清空ARP-a  查看arp缓存arp-s   ip与mac绑定

  4. hadoop各版本下载

    http://hadoop.apache.org/ Download Hadoop from the release page. http://hadoop.apache.org/releases.h ...

  5. hdu 5063 Operation the Sequence

    http://acm.hdu.edu.cn/showproblem.php?pid=5063 思路:因为3查询最多50,所以可以在查询的时候逆操作找到原来的位置,然后再求查询的值. #include ...

  6. JavaScript encodeURI() 函数

    encodeURI() 函数可把字符串作为 URI 进行编码. -------------------------------------------------------------------- ...

  7. c#:类 相关练习;

    1. 2. int i = a.Length;//获取字符串的长度   a = a.ToLower();//将字符串中的大写英文字符转化为小写   a = a.ToUpper();//将字符串中的小写 ...

  8. Oracle索引扫描算法

    SQL> create table t as select * from dba_objects; Table created. SQL> create index idx_t on t( ...

  9. BZOJ 1053 [HAOI2007]反素数ant

    1053: [HAOI2007]反素数ant Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1948  Solved: 1094[Submit][St ...

  10. Delphi NativeXml用法攻略

    NativeXml用法攻略 NativeXml可以在官网上下载,下载后将文件夹放在指定地方,打开DELPHI在其环境变量中引用NativeXml路径,然后在程序中引用NativeXml单元,我们就可以 ...