【转】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. [转载]网络编辑必知常识:什么是PV、UV和PR值 zz

    1.什么是pv PV(page view),即页面浏览量,或点击量;通常是衡量一个网络新闻频道或网站甚至一条网络新闻的主要指标. 高手对pv的解释是,一个访问者在24小时(0点到24点)内到底看了你网 ...

  2. linux主要目录的作用

    手动敲一遍.算是加强记忆吧~ /:文件系统的入口,也是最高一级的目录 /bin:最基本的且着急用户和普通用户都可以使用的命令放在此目录下,如:ls.cp等 /boot:存放Linux的内核及引导系统所 ...

  3. js 外部文件加载处理

    概述 前端在日常工作中很大一部分时间是在思考页面的优化方案,让页面载入得更快.鉴于javascript是单线程的事件驱动语言,优化工作之一就是:控制图片.swf.iframe等大流量文件以及js和cs ...

  4. About JavaScript

    JavaScript Function, Constructor function, Plain Object (expression function, closure) Maintainable ...

  5. SharePoint 2013 WebTemplates

    SharePoint 2013 WebTemplates You are here: Home / SharePoint 2013 WebTemplates   January 24, 2013 Ta ...

  6. Lowest Common Ancestor in Binary Tree

    The problem: Given node P and node Q in a binary tree T. Find out the lowest common ancestor of the ...

  7. POJ1416 Shredding Company(dfs)

    题目链接. 分析: 这题从早上调到现在.也不算太麻烦,细节吧. 每个数字都只有两种状态,加入前一序列和不加入前一序列.DFS枚举. #include <iostream> #include ...

  8. BZOJ 1054 [HAOI2008]移动玩具

    1054: [HAOI2008]移动玩具 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1388  Solved: 764[Submit][Statu ...

  9. HDOJ 2076 夹角有多大(题目已修改,注意读题)

    Problem Description 时间过的好快,一个学期就这么的过去了,xhd在傻傻的看着表,出于对数据的渴望,突然他想知道这个表的时针和分针的夹角是多少.现在xhd知道的只有时间,请你帮他算出 ...

  10. Linux 数学运算

    let 命令 a= b= let c=a+B echo $c let a++ let b++ echo $a $b []方法 a= b= echo $[a+b] echo $[$a+$b] (()) ...