博客
关于我
MySQL JOIN连接用法
阅读量:254 次
发布时间:2019-03-01

本文共 1827 字,大约阅读时间需要 6 分钟。

一、介绍join

SQL join用来把来自两个或多个表的行结合起来,下图展示了LEFT JOIN、RIGHT JOIN、INNER JOIN、OUTER JOIN相关的七种用法:

在这里插入图片描述
不同的SQL join之间的区别:

  • INNER JOIN:如果表中有至少一个匹配,则返回行
  • LEFT JOIN:即使右表中没有匹配,也从左表返回所有的行
  • RIGHT JOIN:即使左表中没有匹配,也从右表返回所有的行
  • FULL JOIN:只要其中一个表中存在匹配,则返回行

二、代码例子

创建数据库:

create database if not exists test default charset utf8 collate utf8_general_ci;use test;SET NAMES utf8;SET FOREIGN_KEY_CHECKS = 0;DROP TABLE IF EXISTS `Websites`;CREATE TABLE `Websites` (  `id` int(11) NOT NULL,  `name` char(20) NOT NULL DEFAULT '',  `url` char(30) NOT NULL DEFAULT '',  `alexa` int(11) NOT NULL,  `country` char(10) NOT NULL DEFAULT '',  PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;DROP TABLE IF EXISTS `access_log`;CREATE TABLE `access_log` (  `aid` int(11) NOT NULL,  `site_id` int(11) NOT NULL,  `count` int(11) NOT NULL,  `date` datetime NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;BEGIN;INSERT INTO `Websites` VALUES ('1', 'Google', 'https://www.google.cm/', '1', 'USA'), ('2', 'TaoBao', 'https://www.taobao.cm/', '13', 'CN'), ('3', 'CaiNiao', 'https://www.runoob.cm/', '4689', 'CN'), ('4', 'Weibo', 'https://www.weibo.cm/', '20', 'CN'), ('5', 'Facebook', 'https://www.facebook.cm/', '3', 'USA'), ('7', 'stackoverflow', 'https://www.stackoverflow.cm/', '0', 'IND');COMMIT;BEGIN;INSERT INTO `access_log` VALUES ('1', '1', '45', '2016-05-10'), ('2', '3', '100', '2016-05-13'), ('3', '1', '230', '2016-05-14'), ('4', '2', '10', '2016-05-14'), ('5', '5', '205', '2016-05-14'), ('6', '4', '13', '2016-05-15'), ('7', '3', '220', '2016-05-15'), ('8', '5', '545', '2016-05-16'), ('9', '3', '201', '2016-05-17');COMMIT;

查看数据库:

在这里插入图片描述
在这里插入图片描述
在这两个表中,Websites表中的id列指向access_log表中的字段site_id,所以上面这两个表可以通过id和site_id联系起来。如果我们运行下面的SQL语句:

SELECT Websites.id, Websites.name, access_log.count, access_log.dateFROM WebsitesINNER JOIN access_logON Websites.id=access_log.site_id;

输出如下:

在这里插入图片描述

转载地址:http://xntx.baihongyu.com/

你可能感兴趣的文章
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named 'pandads'
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No static resource favicon.ico.
查看>>
no such file or directory AndroidManifest.xml
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>