Home > Database > Mysql Tutorial > MYSQL 初学者扫盲_MySQL

MYSQL 初学者扫盲_MySQL

WBOY
Release: 2016-06-01 13:57:28
Original
979 people have browsed it

    先 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);
+―-+―――

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template