search
HomeDatabaseMysql TutorialMySQL 詳解_MySQL

還是先 Create table 吧

create table emp(
  id   int not null primary key,
  name     varchar(10)
);

create table emp_dept(
  dept_id       varchar(4) not null,
  emp_id        int not null,
  emp_name      varchar(10),
  primary key (dept_id,emp_id));


insert into emp() values
(1,"Dennis-1"),
(2,"Dennis-2"),
(3,"Dennis-3"),
(4,"Dennis-4"),
(5,"Dennis-5"),
(6,"Dennis-6"),
(7,"Dennis-7"),
(8,"Dennis-8"),
(9,"Dennis-9"),
(10,"Dennis-10");

insert into emp_dept() values
("R&D",1,"Dennis-1"),
("DEv",2,"Dennis-2"),
("R&D",3,"Dennis-3"),
("Test",4,"Dennis-4"),
("Test",5,"Dennis-5");

>> left join
-------------
select a.id,a.name,b.dept_id
from  emp a left join emp_dept b on (a.id=b.emp_id);

# 挑出左邊的 table emp 中的所有資料,即使 emp_dept 中沒有的資料也挑出來,沒有的就用 NULL 來顯示,
# 也即顯示資料是以左邊的 table emp 中的資料為基礎

mysql> select a.id,a.name,b.dept_id
    -> from  emp a left join emp_dept b on (a.id=b.emp_id);
+----+-----------+---------+
| id | name      | dept_id |
+----+-----------+---------+
|  1 | Dennis-1  | R&D     |
|  2 | Dennis-2  | DEv     |
|  3 | Dennis-3  | R&D     |
|  4 | Dennis-4  | Test    |
|  5 | Dennis-5  | Test    |
|  6 | Dennis-6  | NULL    |
|  7 | Dennis-7  | NULL    |
|  8 | Dennis-8  | NULL    |
|  9 | Dennis-9  | NULL    |
| 10 | Dennis-10 | NULL    |
+----+-----------+---------+

# 挑出 table emp 中有而 table emp_dept 中沒有的人員資料
select a.id,a.name,b.dept_id
from emp a  left join  emp_dept b on (a.id=b.emp_id)
where b.dept_id IS NULL;

mysql> select a.id,a.name,b.dept_id
    -> from emp a  left join  emp_dept b on (a.id=b.emp_id)
    -> where b.dept_id IS NULL;
+----+-----------+---------+
| id | name      | dept_id |
+----+-----------+---------+
|  6 | Dennis-6  | NULL    |
|  7 | Dennis-7  | NULL    |
|  8 | Dennis-8  | NULL    |
|  9 | Dennis-9  | NULL    |
| 10 | Dennis-10 | NULL    |
+----+-----------+---------+

# 把 table emp_dept 放在左邊的情形(當然以 emp_dept 中的數據為基礎來顯示資料,emp 中比emp_dept 中多的資料也就不會顯示出來了):

select a.id,a.name,b.dept_id
from  emp_dept b left join  emp a on (a.id=b.emp_id);
mysql> select a.id,a.name,b.dept_id
    -> from  emp_dept b left join  emp a on (a.id=b.emp_id);
+------+----------+---------+
| id   | name     | dept_id |
+------+----------+---------+
|    2 | Dennis-2 | DEv     |
|    1 | Dennis-1 | R&D     |
|    3 | Dennis-3 | R&D     |
|    4 | Dennis-4 | Test    |
|    5 | Dennis-5 | Test    |
+------+----------+---------+

>> right join
---------------
select a.id,a.name,b.dept_id
from  emp a right join emp_dept b on (a.id=b.emp_id);
# 挑資料時以右邊 table emp_dept 中的資料為基礎來顯示資料

mysql> select a.id,a.name,b.dept_id
    -> from  emp a right join emp_dept b on (a.id=b.emp_id);
+------+----------+---------+
| id   | name     | dept_id |
+------+----------+---------+
|    2 | Dennis-2 | DEv     |
|    1 | Dennis-1 | R&D     |
|    3 | Dennis-3 | R&D     |
|    4 | Dennis-4 | Test    |
|    5 | Dennis-5 | Test    |
+------+----------+---------+
5 rows in set (0.00 sec)

# 我們再把 table 的位置交換一下,再用 right join 試試

select a.id,a.name,b.dept_id
from  emp_dept b  right join emp a on (a.id=b.emp_id);

mysql> select a.id,a.name,b.dept_id
    -> from  emp_dept b  right join emp a on (a.id=b.emp_id);
+----+-----------+---------+
| id | name      | dept_id |
+----+-----------+---------+
|  1 | Dennis-1  | R&D     |
|  2 | Dennis-2  | DEv     |
|  3 | Dennis-3  | R&D     |
|  4 | Dennis-4  | Test    |
|  5 | Dennis-5  | Test    |
|  6 | Dennis-6  | NULL    |
|  7 | Dennis-7  | NULL    |
|  8 | Dennis-8  | NULL    |
|  9 | Dennis-9  | NULL    |
| 10 | Dennis-10 | NULL    |
+----+-----------+---------+

# 是不是和 left join 一樣了?

>> direct join
--------------
# 如果用right join 同不用 Join 直接挑資料是相同的,它等介於以下的指令

select a.id,a.name,b.dept_id
from  emp a ,emp_dept b
where a.id=b.emp_id;


mysql> select a.id,a.name,b.dept_id
    -> from  emp a ,emp_dept b
    -> where a.id=b.emp_id;
+----+----------+---------+
| id | name     | dept_id |
+----+----------+---------+
|  2 | Dennis-2 | DEv     |
|  1 | Dennis-1 | R&D     |
|  3 | Dennis-3 | R&D     |
|  4 | Dennis-4 | Test    |
|  5 | Dennis-5 | Test    |
+----+----------+---------+


怎樣,弄明白了嗎?

Enjoy it!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
华硕b450主板如何安装Windows 11华硕b450主板如何安装Windows 11Dec 31, 2023 am 10:10 AM

华硕b450是一块非常优秀的主板,有着众多的用户,如果大家想要在这款主板之上安装win11系统,需要开启tpm2.0和安全启动选项,很多朋友可能不知道怎么开启,下面小编就跟大家来看看如何进行操作吧。华硕b450怎么安装win11:1、首先我们重启系统,使用“F2”进入bios设置,然后点击右下角“高级模式”进入。2、然后在“启动”选项下进入“安全启动”3、接着在“操作系统类型”右侧选择“WindowsUEFI模式”4、设置完成后点击右上角“search”进入搜索。5、然后在其中搜索“PTT”并点

MySql中如何使用JOINMySql中如何使用JOINJun 04, 2023 am 08:02 AM

JOIN的含义就如英文单词“join”一样,连接两张表,大致分为内连接,外连接,右连接,左连接,自然连接。先创建两个表,下面用于示例CREATETABLEt_blog(idINTPRIMARYKEYAUTO_INCREMENT,titleVARCHAR(50),typeIdINT);SELECT*FROMt_blog;+----+-------+--------+|id|title|typeId|+----+-------+--------+|1|aaa|1||2|bbb|2||3|ccc|3|

MySQL Join使用原理是什么MySQL Join使用原理是什么May 26, 2023 am 10:07 AM

Join的类型leftjoin,以左表为驱动表,以左表作为结果集基础,连接右表的数据补齐到结果集中rightjoin,以右表为驱动表,以右表作为结果集基础,连接左表的数据补齐到结果集中innerjoin,结果集取两个表的交集fulljoin,结果集取两个表的并集mysql没有fulljoin,union取代union与unionall的区别为,union会去重crossjoin笛卡尔积如果不使用where条件则结果集为两个关联表行的乘积与,的区别为,crossjoin建立结果集时会根据on条件过

undefined和null是什么意思undefined和null是什么意思Nov 20, 2023 pm 02:39 PM

在JavaScript 中,undefined和null都代表着“无”的概念:1、undefined 表示一个未初始化的变量或一个不存在的属性,当声明了一个变量但没有对其赋值时,这个变量的值就是undefined,访问对象中不存在的属性时,返回的值也是undefined;2、null表示一个空的对象引用,在某些情况下,可以将对象的引用设置为null,以便释放其占用的内存。

c语言中null和NULL的区别是什么c语言中null和NULL的区别是什么Sep 22, 2023 am 11:48 AM

c语言中null和NULL的区别是:null是C语言中的一个宏定义,通常用来表示一个空指针,可以用于初始化指针变量,或者在条件语句中判断指针是否为空;NULL是C语言中的一个预定义常量,通常用来表示一个空值,用于表示一个空的指针、空的指针数组或者空的结构体指针。

什么时候用null和undefined什么时候用null和undefinedNov 13, 2023 pm 02:11 PM

null和undefined都表示缺少值或未定义的状态,根据使用场景的不同,选择使用null还是undefined有以下一些指导原则:1、当需要明确指示一个变量为空或无效时,可以使用null;2、当一个变量已经声明但尚未赋值时,会被默认设置为undefined;3、当需要检查一个变量是否为空或未定义时,使用严格相等运算符“===”来判断变量是否为null或undefined。

null和undefined有什么不同null和undefined有什么不同Nov 08, 2023 pm 04:43 PM

null和undefined的区别在:1、语义含义;2、使用场景;3、与其它值的比较;4、与全局变量的关系;5、与函数参数的关系;6、可空性检查;7、性能考虑;8、在JSON序列化中的表现;9、与类型的关系。详细介绍:1、语义含义,null通常表示知道这个变量不会拥有任何有效的对象值,而undefined则通常表示变量未被赋值,或者对象没有此属性;2、使用场景等等。

mysql的join查询和多次查询方法是什么mysql的join查询和多次查询方法是什么Jun 02, 2023 pm 04:29 PM

join查询和多次查询比较MySQL多表关联查询效率高点还是多次单表查询效率高?在数据量不够大的时候,用join没有问题,但是一般都会拉到service层上去做第一:单机数据库计算资源很贵,数据库同时要服务写和读,都需要消耗CPU,为了能让数据库的吞吐变得更高,而业务又不在乎那几百微妙到毫秒级的延时差距,业务会把更多计算放到service层做,毕竟计算资源很好水平扩展,数据库很难啊,所以大多数业务会把纯计算操作放到service层做,而将数据库当成一种带事务能力的kv系统来使用,这是一种重业务,

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),