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. Add skip-grant-tables to the my.ini file and r...
This article shares the installation and configur...
Time always passes surprisingly fast without us n...
I believe everyone is familiar with database inde...
Table of contents 1. Template 2. Generics 3. Gene...
Since my development environment is to install Ce...
Here is an example code for using regular express...
Six steps of JDBC: 1. Register the driver 2. Get ...
Table of contents variable Data Types Extension P...
1. Introduction The EXPLAIN statement provides in...
Table of contents 1. Four concepts 1. JavaScript ...
1. Environment version Docker version 19.03.12 ce...
This article describes how to install mysql5.7.16...
1. Fixed width + adaptive Expected effect: fixed ...
Today we analyzed another typical problem about d...