当前位置:网站首页>MySQL multi table query_ Cartesian product_ Inner connection_ Implicit connection
MySQL multi table query_ Cartesian product_ Inner connection_ Implicit connection
2022-07-23 16:27:00 【Camellia——】
-- What is multi table query : Query multiple fields in multiple tables
CREATE DATABASE IF NOT EXISTS mydb_02;
USE mydb_02;
CREATE TABLE dept (
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR (20)
);
INSERT INTO dept (NAME)
VALUES
(' Development Department '),
(' The Marketing Department '),
(' Finance Department ') ;
SELECT *FROM dept;
DROP TABLE emp;
-- Create an employee table
CREATE TABLE emp (
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR (10),
gender CHAR(1),-- Gender
salary DOUBLE,-- Wages
join_date DATE,-- Date of entry
dept_id INT,
FOREIGN KEY (dept_id) REFERENCES dept (id) -- Foreign keys , Related department table ( The primary key of the Department table ) )
) ;
INSERT INTO emp(NAME,gender,salary,join_date,dept_id) VALUES(' The Monkey King ',' male ',7200,'2013-02-24',1);
INSERT INTO emp(NAME,gender,salary,join_date,dept_id) VALUES(' Pig eight quit ',' male ',3600,'2010-12-02',2);
INSERT INTO emp(NAME,gender,salary,join_date,dept_id) VALUES(' Tang's monk ',' male ',9000,'2008-08-08',2);
INSERT INTO emp(NAME,gender,salary,join_date,dept_id) VALUES(' Bones jing ',' Woman ',5000,'2015-10-07',3);
INSERT INTO emp(NAME,gender,salary,join_date,dept_id) VALUES(' Spider essence ',' Woman ',4500,'2011-03-14',1);-- Single table query
-- Look up the employee tableSELECT *FROM emp;
SELECT *FROM dept;-- Multi-table query Query multiple tables
-- demand : Query all fields in the employee and department tables at the same time
-- A problem : Is a Cartesian product (A All records in the table *B All records in the table )--- Field redundancy
SELECT*FROM emp,dept;-- Solve Cartesian product
-- 1) Which tables to query The employee table emp, Departmental table dept
-- 2) Query which fields in these tables Employee list name , Department number , Names of all departments in the Department table
-- 3) The link conditions of these tables In the list of employees dept_id To associate the primary key of the Department table id-- Internal connection
-- Implicit inner join Use Basic where sentence Connection condition
-- Display inner connection inner join Connection condition-- demand : Use implicit inner join queries : Name in the employee table , department id Check at the same time Department name in the Department table
-- surface :emp ,dept
-- emp name,dept_id
-- dept name
-- emp Of dept_id = dept Of id
SELECT
emp.`name`,
emp.`dept_id`,
dept.`name`
FROM
emp,
dept
WHERE emp.`dept_id` = dept.`id` ;-- improvement : Alias the table , Simplify writing
SELECT
t1.`name`,
t1.`dept_id`,
t2.`name`
FROM
emp t1,
dept t2
WHERE t1.`dept_id` = t2.`id` ;
边栏推荐
- VRRP+MSTP配置详解【华为eNSP实验】
- 为什么使用opengaussjdbc的时候老是出现FATAL?(标签-数据库|关键词-user)
- Why is apple x charging slowly_ IPhone 12 supports 15W MagSafe wireless charging. What will happen to iPhone charging in the future_ Charger
- W3C introduces decentralized identifier as web standard
- sqlnet. Ora-12154 and ora-01017 connection exceptions caused by incorrect ora file settings
- Bean Validation规范篇----03
- Ora-01654 error: table space is full, insert failed
- Bean Validation核心組件篇----04
- GO语言学习——复习包、接口、文件操作
- VMWARE平台STS证书过期
猜你喜欢

千万别让富坚义博看到这个

lc marathon 7.23

聊一聊JVM的内存布局

lc marathon 7.23

2022蓝帽杯初赛wp

MySQL - master-slave replication

7、 Logic of JMeter sending request

20220721挨揍内容

问题随记 —— 无法打开包括文件: “dirent.h”: No such file or directory

Middle aged crisis, retired at the age of 35, what do migrant workers take to compete with capitalists?
随机推荐
Niuke-top101-bm35
现代商业无代码开发平台的治理和网络安全
Oracle中实现删除指定查询条件的所有数据
2022蓝帽杯初赛wp
反转链表画图演示
死锁、饥饿、死循环之间的区别
[suctf 2018]multisql (MySQL precompiled)
[2023 approved in advance] BOE
SharedPreferences data storage
How beautiful can VIM be configured?
Why is apple x charging slowly_ IPhone 12 supports 15W MagSafe wireless charging. What will happen to iPhone charging in the future_ Charger
Redis installation
C#中单例模式的实现
ICML 2022 | sparse double decline: can network pruning also aggravate model overfitting?
EmguCV录制视频
信箱通信-
Please initialize the log4j system properly.
vulnstack红日-4
mysql多表查询之_内连接_显示内连接
Bean validation beginner ----02