当前位置:网站首页>Multi table operation inner join query
Multi table operation inner join query
2022-07-04 19:41:00 【Soup key TJ】
Catalog
Multi table query classification
Multi table query data preparation
Multi table query classification
- Internal connection query
- External connection query
- Subquery
- Self association query
Multi table query data preparation
- Create a total of 5 Tables
- They are user tables , The order sheet , Classification list , Commodity list , In the middle of table
-- establish user surface CREATE TABLE user( id INT PRIMARY KEY auto_increment, -- user id name VARCHAR(20), -- User name age INT -- User age ); -- Add data INSERT INTO user VALUES (1,' Zhang San ',23); INSERT INTO user VALUES (2,' Li Si ',24); INSERT INTO user VALUES (3,' Wang Wu ',25); INSERT INTO user VALUES (4,' Zhao Liu ',26); -- Create order form CREATE TABLE orderlist( id INT PRIMARY KEY auto_increment, -- Order id number VARCHAR(20), -- The order no. uid INT, -- Foreign key field CONSTRAINT ou FOREIGN KEY (uid) REFERENCES user(id) ); -- Add data INSERT INTO orderlist VALUES (1,'hm001',1); INSERT INTO orderlist VALUES (2,'hm002',1); INSERT INTO orderlist VALUES (3,'hm003',2); INSERT INTO orderlist VALUES (4,'hm004',2); INSERT INTO orderlist VALUES (5,'hm005',3); INSERT INTO orderlist VALUES (6,'hm006',3); INSERT INTO orderlist VALUES (7,'hm007',NULL); -- Create commodity classification table CREATE TABLE category( id INT PRIMARY KEY auto_increment, -- Classification of goods id NAME VARCHAR(20) -- Category name ); -- Add data INSERT INTO category VALUES (1,' Mobile phone digital '); INSERT INTO category VALUES (2,' Computer office '); INSERT INTO category VALUES (3,' Tobacco, wine, tea and sugar '); INSERT INTO category VALUES (4,' Shoes and bags '); -- Create a product list CREATE TABLE product( id INT PRIMARY KEY auto_increment, -- goods id NAME VARCHAR(20), -- Name of commodity cid INT, -- Foreign key field CONSTRAINT cp_1 FOREIGN KEY (cid) REFERENCES category(id) ); -- Add data INSERT INTO product VALUES (1,' Huawei mobile phones ',1); INSERT INTO product VALUES (2,' Mi phones ',1); INSERT INTO product VALUES (3,' Lenovo computer ',2); INSERT INTO product VALUES (4,' Apple computer ',2); INSERT INTO product VALUES (5,' Chinese cigarettes ',3); INSERT INTO product VALUES (6,' Yuxi cigarette ',3); INSERT INTO product VALUES (7,' Family planning supplies ',NULL); -- Create middle table CREATE TABLE us_pro( upid INT PRIMARY KEY auto_increment, -- In the middle of table id uid INT, -- Foreign key field , It needs to be associated with the primary key of the user table pid INT, -- Foreign key field , It needs to be associated with the primary key of the commodity table CONSTRAINT up_1 FOREIGN KEY (uid) REFERENCES user(id), CONSTRAINT up_2 FOREIGN KEY (pid) REFERENCES product(id) ); -- Add data INSERT INTO us_pro VALUES (NULL,1,1); INSERT INTO us_pro VALUES (NULL,1,2); INSERT INTO us_pro VALUES (NULL,1,3); INSERT INTO us_pro VALUES (NULL,1,4); INSERT INTO us_pro VALUES (NULL,1,5); INSERT INTO us_pro VALUES (NULL,1,6); INSERT INTO us_pro VALUES (NULL,1,7); INSERT INTO us_pro VALUES (NULL,2,1); INSERT INTO us_pro VALUES (NULL,2,2); INSERT INTO us_pro VALUES (NULL,2,3); INSERT INTO us_pro VALUES (NULL,2,4); INSERT INTO us_pro VALUES (NULL,2,5); INSERT INTO us_pro VALUES (NULL,2,6); INSERT INTO us_pro VALUES (NULL,2,7); INSERT INTO us_pro VALUES (NULL,3,1); INSERT INTO us_pro VALUES (NULL,3,2); INSERT INTO us_pro VALUES (NULL,3,3); INSERT INTO us_pro VALUES (NULL,3,4); INSERT INTO us_pro VALUES (NULL,3,5); INSERT INTO us_pro VALUES (NULL,3,6); INSERT INTO us_pro VALUES (NULL,3,7); INSERT INTO us_pro VALUES (NULL,4,1); INSERT INTO us_pro VALUES (NULL,4,2); INSERT INTO us_pro VALUES (NULL,4,3); INSERT INTO us_pro VALUES (NULL,4,4); INSERT INTO us_pro VALUES (NULL,4,5); INSERT INTO us_pro VALUES (NULL,4,6); INSERT INTO us_pro VALUES (NULL,4,7);
Query principle
- Inner join queries are part of the data where two tables intersect ( Data associated with primary and foreign keys )
The query syntax
- Explicit inner connection
- select Name from Table name 1 [inner] join Table name 2 on Conditions
- Implicit inner join
- select Name from Table name 1, Table name 2 where Conditions
Relevant concepts
- Self join
- Concept : It is a special equivalent connection in inner connection query , The so-called self connection refers to the connection between a table and its current table
- The biggest feature : One table is regarded as two tables
- Connect yourself
- Equivalent connection
- Is in the keyword on The matching condition after is through = To achieve ;
- Equivalent connection is usually a table connecting another table
- Unequal value connection
- In keywords on Use in the post matching condition except = Inequality conditions implemented by relational operators other than (> >= < <= !=)
Example operation
- Explicit inner connection
- Query user information and corresponding order information
- Query user information and corresponding order information , names
- Query user name , Age and order number
-- Explicit inner connection -- Query user information and corresponding order information SELECT * FROM user INNER JOIN orderlist ON orderlist.uid=user.id; -- Query user information and corresponding order information , names SELECT * FROM user u INNER JOIN orderlist o ON o.uid=u.id; -- Query user name , Age and order number SELECT u.name, -- User name u.age, -- User age o.number -- The order no. FROM user u -- User table INNER JOIN orderlist o -- The order sheet ON o.uid=u.id;
- Implicit inner join
- Query user name , Age and order number
-- Implicit inner join -- Query user name , Age and order number SELECT u.name, -- User name u.age, -- User age o.number -- The order no. FROM user u, -- User table orderlist o -- The order sheet WHERE o.uid=u.id;
边栏推荐
- kotlin 循环控制
- "Only one trip", active recommendation and exploration of community installation and maintenance tasks
- Shell 編程核心技術《四》
- 基于NCF的多模块协同实例
- 关于判断点是否位于轮廓内的一点思考
- HDU 6440 2018中国大学生程序设计网络选拔赛
- Shell programming core technology II
- [QNX Hypervisor 2.2用户手册]6.3.1 工厂页和控制页
- 92. (cesium chapter) cesium building layering
- mysql中explain语句查询sql是否走索引,extra中的几种类型整理汇总
猜你喜欢
Upgrade the smart switch, how much is the difference between the "zero fire version" and "single fire" wiring methods?
English语法_名词 - 使用
多表操作-内连接查询
English grammar_ Noun - use
Stream stream
Mysql database basic operation -ddl | dark horse programmer
SSRS筛选器的IN运算(即包含于)用法
联想首次详解绿色智城数字孪生平台 破解城市双碳升级难点
OpenCV的二值化处理函数threshold()详解
Comment utiliser async awati asynchrone Task Handling au lieu de backgroundworker?
随机推荐
1011 World Cup betting (20 points) (pat a)
如何使用Async-Awati异步任务处理代替BackgroundWorker?
Niuke Xiaobai month race 7 F question
Functional interface
黑马程序员-软件测试--07阶段2-linux和数据库-09-24-linux命令学习步骤,通配符,绝对路径,相对路径,文件和目录常用命令,文件内容相关操作,查看日志文件,ping命令使用,
Shell 编程核心技术《三》
Introduction to polyfit software
黑马程序员-软件测试--08阶段2-linux和数据库-23-30-进程端口相关,修改文件权限,端口号信息的获取,程序和进程相关操作,linux命令案例
矩阵翻转(数组模拟)
HDU 6440 2018中国大学生程序设计网络选拔赛
1002. A+b for Polynomials (25) (PAT class a)
92.(cesium篇)cesium楼栋分层
FPGA timing constraint sharing 01_ Brief description of the four steps
添加命名空间声明
黑马程序员-软件测试--09阶段2-linux和数据库-31-43修改文件权限字母发的说明,-查找链接修改文件,查找文件命令,链接文件,压缩解压方式,vi编辑器基本使用,
To sort out messy header files, I use include what you use
1003 emergency (25 points) (PAT class a)
"Only one trip", active recommendation and exploration of community installation and maintenance tasks
记一次 .NET 某工控数据采集平台 线程数 爆高分析
BCG 使用之CBCGPProgressDlgCtrl进度条使用