MySQL multi-table query Add a worksheet -- User table (user) CREATE TABLE `user`( `id` INT AUTO_INCREMENT PRIMARY KEY COMMENT 'User id (primary key)', `username` VARCHAR(50) COMMENT 'User name', `age` CHAR(3) COMMENT 'User age' ); -- Orders CREATE TABLE `orders`( `id` INT AUTO_INCREMENT PRIMARY KEY COMMENT 'Order ID (primary key)', `price` DOUBLE COMMENT 'Order price', `user_id` INT COMMENT 'User id (foreign key)' ); -- Add a foreign key to an existing table. The syntax is as follows: alter table table name add constraint [foreign key name] foreign key (foreign key field) references parent table (primary key field); ALTER TABLE orders ADD CONSTRAINT user_fk FOREIGN KEY (user_id) REFERENCES `user` (id); -- Add data to the user table INSERT INTO USER VALUES(1,'第一',11); INSERT INTO USER VALUES(2,'小等',12); INSERT INTO USER VALUES(3,'张三',33); INSERT INTO USER VALUES(4,'李四',24); INSERT INTO USER VALUES(5,'王五',17); INSERT INTO USER VALUES(6,'赵六',36); INSERT INTO USER VALUES(7,'七七',18); INSERT INTO USER VALUES(8,'粑粑',NULL); -- Insert data into the orders table INSERT INTO orders VALUES(111,1314,3); INSERT INTO orders VALUES(112,122,3); INSERT INTO orders VALUES(113,15,4); INSERT INTO orders VALUES(114,315,5); INSERT INTO orders VALUES(115,1014,NULL); INSERT INTO orders VALUES(116,666,6); INSERT INTO orders VALUES(117,1111,1); INSERT INTO orders VALUES(118,8888,NULL); Cartesian Product
SELECT * FROM `user`,`orders`;
SELECT * FROM `user` AS u,`orders` AS o WHERE u.`id`=o.`user_id`; 1. Inner Join 1.1 Implicit inner join
SELECT * FROM `user` AS u,`orders` AS o WHERE u.`id`=o.`user_id`; 1.2 Display inner join (recommended)
SELECT * FROM `user` u JOIN `orders` o ON u.`id`=o.`user_id` WHERE age >= 18; 2. Outer Join
2.1 Right external links
SELECT * FROM `user` u RIGHT JOIN `orders` o ON u.`id`=o.`user_id`; Left table data (user) The data in the right table (orders) 2.2 Left outer link (recommended)
SELECT * FROM `user` u LEFT JOIN `orders` o ON u.`id`=o.`user_id`; Left table data (user) The data in the right table (orders) 3. Subquery
SELECT * FROM orders o WHERE o.`user_id` IN ( SELECT u.`id` FROM `user` u WHERE u.`age` IN ( SELECT MAX(u.`age`) FROM `user` u ) ); 4. Full connection (not supported by MySQL)
For other MySQL articles, please see the following links MySQL DDL statements MySQL CRUD statements MySQL Aggregate Functions MySQL multi-table query END… This is the end of this article about advanced query and multi-table query of MySQL database. For more relevant MySQL advanced query and multi-table query content, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Detailed explanation of the use cases of Vue listeners
>>: Use Docker to build a Redis master-slave replication cluster
1. Multiple borders[1] Background: box-shadow, ou...
Preface This article mainly summarizes some of th...
Table of contents 1. First look at COUNT 2. The d...
Here is a text hovering and jumping effect implem...
Preface This article will explain how Vue compone...
1. What is HTML HTML (HyperText Markup Language):...
Preface Nginx (pronounced "engine X") i...
Application example website http://www.uhuigou.net...
This article summarizes some simple principles of...
1. MySQL 1.1 MySQL installation mysql-5.5.27-winx...
Note: I use Centos to install docker Step 1: Inst...
Dark background style page design is very popular...
exhibit design Password strength analysis The pas...
Table of contents 1. What is the life cycle 2. Th...
Why optimize: With the launch of the actual proje...