Home Database Mysql Tutorial MySQL 詳解_MySQL

MySQL 詳解_MySQL

Jun 01, 2016 pm 02:06 PM
b join null

還是先 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 of this Website
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to install Windows 11 on ASUS b450 motherboard How to install Windows 11 on ASUS b450 motherboard Dec 31, 2023 am 10:10 AM

ASUS b450 is a very excellent motherboard with many users. If you want to install the win11 system on this motherboard, you need to enable tpm2.0 and the secure boot option. Many friends may not know how to enable it. The editor below will Let’s take a look at how to operate it. How to install win11 on ASUS b450: 1. First, we restart the system, use "F2" to enter the bios settings, and then click "Advanced Mode" in the lower right corner to enter. 2. Then enter "Secure Boot" under the "Startup" option 3. Then select "Windows UEFI Mode" to the right of "Operating System Type" 4. After the settings are completed, click "search" in the upper right corner to enter the search. 5. Then search for “PTT” and click

How to use JOIN in MySql How to use JOIN in MySql Jun 04, 2023 am 08:02 AM

The meaning of JOIN is just like the English word "join". It joins two tables and can be roughly divided into inner join, outer join, right join, left join and natural join. First create two tables, the following is used as an example CREATETABLEt_blog(idINTPRIMARYKEYAUTO_INCREMENT,titleVARCHAR(50),typeIdINT);SELECT*FROMt_blog;+----+------+--------+| id|title|typeId|+----+-------+--------+|1|aaa|1||2|bbb|2||3|ccc|3|

What is the difference between null and NULL in c language What is the difference between null and NULL in c language Sep 22, 2023 am 11:48 AM

The difference between null and NULL in C language is: null is a macro definition in C language, usually used to represent a null pointer, which can be used to initialize pointer variables, or to determine whether the pointer is null in a conditional statement; NULL is a macro definition in C language A predefined constant in , usually used to represent a null value, used to represent a null pointer, null pointer array or null structure pointer.

What is the usage principle of MySQL Join? What is the usage principle of MySQL Join? May 26, 2023 am 10:07 AM

Join type leftjoin uses the left table as the driving table and the left table as the basis of the result set. The data from the right table is connected to the result set. rightjoin uses the right table as the driving table and the right table as the basis of the result set to connect the left table. The data is added to the result set innerjoin. The result set takes the intersection of the two tables fulljoin. The result set takes the union of the two tables. MySQL does not have a fulljoin. The difference between union and unionall is that union will deduplicate the crossjoin Cartesian product. If the where condition is not used, the result set will be the product and of the two associated table rows. The difference is that when crossjoin creates the result set, it will be passed according to the on condition.

What do undefined and null mean? What do undefined and null mean? Nov 20, 2023 pm 02:39 PM

In JavaScript, both undefined and null represent the concept of "nothing": 1. undefined represents an uninitialized variable or a non-existent property. When a variable is declared but no value is assigned to it, the value of the variable is undefined , when accessing properties that do not exist in the object, the returned value is also undefined; 2. null represents an empty object reference. In some cases, the object reference can be set to null to release the memory it occupies.

What is the difference between null and undefined What is the difference between null and undefined Nov 08, 2023 pm 04:43 PM

The difference between null and undefined is: 1. Semantic meaning; 2. Usage scenarios; 3. Comparison with other values; 4. Relationship with global variables; 5. Relationship with function parameters; 6. Nullability check; 7. Performance considerations; 8. Performance in JSON serialization; 9. Relationship with types. Detailed introduction: 1. Semantic meaning, null usually means knowing that this variable will not have any valid object value, while undefined usually means that the variable has not been assigned a value, or the object does not have this attribute; 2. Usage scenarios, etc.

BITGet official website installation (2025 beginner's guide) BITGet official website installation (2025 beginner's guide) Feb 21, 2025 pm 08:42 PM

BITGet is a cryptocurrency exchange that provides a variety of trading services including spot trading, contract trading and derivatives. Founded in 2018, the exchange is headquartered in Singapore and is committed to providing users with a safe and reliable trading platform. BITGet offers a variety of trading pairs, including BTC/USDT, ETH/USDT and XRP/USDT. Additionally, the exchange has a reputation for security and liquidity and offers a variety of features such as premium order types, leveraged trading and 24/7 customer support.

When to use null and undefined When to use null and undefined Nov 13, 2023 pm 02:11 PM

Both null and undefined indicate a lack of value or an undefined state. Depending on the usage scenario, there are some guiding principles for choosing to use null or undefined: 1. When you need to clearly indicate that a variable is empty or invalid, you can use null; 2. When a variable has been declared but not yet assigned a value, it will be set to undefined by default; 3. When you need to check whether a variable is empty or undefined, use the strict equality operator "===" to determine whether the variable is null or undefined. .

See all articles