当前位置:网站首页>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` ;
边栏推荐
- Redis key has no expiration time set. Why was it actively deleted
- Who is responsible for the problems of virtual anchor and idol endorsement products? Lawyer analysis
- Éléments de base de la validation des haricots - 04
- Oracle中实现删除指定查询条件的所有数据
- Basic concept and deployment of kubernetes
- Using mobile phones too much may lose your job
- [SUCTF 2018]MultiSQL(mysql预编译)
- Mysql客户端到服务端字符集的转换
- 大端模式和小端模式的记忆方法
- First hello of SOC_ World experiment
猜你喜欢

Kubernetes 基本概念和部署

Flutter | 给 ListView 添加表头表尾最简单的方式

Oracle中实现删除指定查询条件的所有数据

Unity Metaverse(一)、Ready Player Me & Blender 自定义你的Avatar虚拟人

Memory methods of big end mode and small end mode

W3C 推出去中心化标识符作为 Web 标准

死锁、饥饿、死循环之间的区别

Bean validation core components - 04

SharedPreferences data storage

Bean Validation核心組件篇----04
随机推荐
Bean Validation起源篇----01
946. Verify stack sequence ●● & sword finger offer 31. stack push in and pop-up sequence ●●
1060 Are They Equal
Calendar日历类
Why does fatal always appear when using opengaussjdbc? (tag database keyword user)
SOC的第一个Hello_World实验
lc marathon 7.23
MySQL-字符串按照数值排序
Éléments de base de la validation des haricots - 04
Leetcode high frequency question: the array can be changed into a non descending state after at least several operations
牛客-TOP101-BM36
国产AI蛋白质结构预测再现突破,用单条序列解决3D结构,彭健团队:“AlphaFold2以来最后一块拼图补齐了”...
AWS 6 AWS IOT
反转链表画图演示
Of the 24 almost inevitable JVM interview questions, I only know 7. How many can you answer?
W3C introduces decentralized identifier as web standard
Dark horse programmer - interface test - four day learning interface test - third day - advanced usage of postman, export and import of Newman case set, common assertions, assertion JSON data, working
nport串口服务器原理,MOXA串口服务器NPORT-5130详细配置
EmguCV录制视频
Packaging and use of fmdb