Many times in actual business we don’t just query a table.
In the e-commerce system, query which users have not purchased products.
The bank may query violation records, and at the same time query the user's
Query the winning information and the basic information of the winner.
The above is just a column situation, so we need to query the two tables together.
In the above business, multiple tables need to be joined together to query to get results, and the essence of multi-table joint query is: table connection.
Table connection
When you need to query fields in multiple tables, you can use table connection to achieve it. Table joins are divided into inner joins and outer joins.
Inner join: Join those records whose fields in the two tables have a join relationship that match the join relationship to form a record set.
Outer join: Other unmatched records will be selected and divided into outer left join and outer right join.
Before learning the experiment, I prepared two simulated data tables for everyone:
- User table to store user information
- Order table, which stores which user purchased which product
user table creation statement
##CREATE TABLE IF NOT EXISTS user (
uid int(11) NOT NULL,
username varchar(30) NOT NULL,
password char(32) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS order_goods (
oid int(11) NOT NULL,
uid int(11) NOT NULL,
name varchar(50) NOT NULL,
buytime int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
user table data is as follows:
##uidusername | password | |
##1京天 | 123456 | | 2
王小二 | 245667 | | 3
王宝强 | 1235531 | | 4
Jing Boran | 123455 | ##5 |
Fan Bingbing5abcwa | | 6 |
黄晓明abcdeef | | 7 |
anglebaby caption | | 8 |
TFBOYSabcdwww | | ##9 | An Xiaochao
12tfddwd | | 10 | Gao Xiaofeng
3124qwqw | | 11 | 李小强
323fxfvdvd | | 12 | 李小超
311aqqee | | 13 | 汉小平
121rcfwrfq | ##14 | 宋小康 |
123123tcsd | 15 | Tong Xiaogang |
3cxvdfs | order_goods data is as follows: oid | ##uid | name | buytime |
---|
1 | 10 | Apple Mouse | 1212313 | 2 | 3 | iphone 12s | 123121241 | ##312 | Sprite | 13232333 | | 415 | | ##34242123
| | 5 3 | iphone keyboard | 12123413 | | ##Note: is above The uid in the order_goods table refers to the uid field in the user table. In the above table, the data row with oid is 1 and the user with uid is 10. For the user with uid 10 in the user table: Gao Xiaofeng. The user purchased an Apple mouse. The purchase time buytime is a unix timestamp.
Inner connectionBasic syntax 1: CategoryDetailed explanation | | Basic syntaxselect table 1.field [as alias], table n. field from table 1 [alias], table n where condition; | Example | select user.uid ,user.username as username,order_goods.oid,order_goods.uid,order_goods.name as shopname from user,order_goods where user.uid = order_goods.uid ; | Example description | Query which users in the product table have purchased products and display the user information | Note: In the following example, the from table uses table aliases.
Because the table name is too long, it is easy to make mistakes every time you write it. We can follow the table directly with an abbreviated English string. When splicing fields earlier, just use the abbreviation string.field. mysql> select u.uid ,u.username as username,o.oid,o.uid,o.name as shopname from user u,order_goods o where u.uid = o.uid;
+-----+-----------+-----+-----+---------------+
| uid | username | oid | uid | shopname |
+-----+-----------+-----+-----+---------------+
| 10 | Gao Xiaofeng | 1 | 10 | Apple Mouse |
| 3 | Wang Baoqiang | 2 | 3 | iphone 12s |
| 12 | Li Xiaochao | 3 | 12 | Sprite |
| 15 | Tong Xiaogang | 4 | 15 | |
| 3 | Li Wenkai | 5 | 3 | iphone keyboard |
+-----+-----------+-----+-----+---------------+
5 rows in set (0.00 sec)
Basic syntax 2: Category | Detailed explanation |
---|
Basic syntax | select table 1. field [as alias], table n. field from table 1 INNER JOIN table n on condition; | Example | select user.uid ,user.username as username,order_goods.oid,order_goods.uid,order_goods.name as shopname from user inner join order_goods on user.uid = order_goods .uid; | Example description | Query which users in the product table have purchased products and display the user information |
The result is consistent with Basic Grammar 1. mysql> select user.uid ,user.username as username,order_goods.oid,order_goods.uid,order_goods.name as shopname from user inner join order_goods on user.uid = order_goods. uid;
+-----+-----------+-----+-----+---------------+
| uid | username | oid | uid | shopname |
+-----+-----------+-----+-----+---------------+
| 10 | Gao Xiaofeng | 1 | 10 | Apple Mouse |
| 3 | Wang Baoqiang | 2 | 3 | iphone 12s |
| 12 | Li Xiaochao | 3 | 12 | Sprite |
| 15 | Tong Xiaogang | 4 | 15 | |
| 3 | Wang Baoqiang | 5 | 3 | iphone keyboard |
+-----+-----------+-----+-----+---------------+
5 rows in set (0.00 sec)
Outer connection##Explanation | Detailed explanation | Basic syntax | select table 1. field [as alias], table n. field from table 1 LEFT JOIN table n on conditions; | Example | select * from user left join order_goods on user.uid = order_goods.uid; | Example description | Take the left as Mainly, check which users have not purchased goods and display the user information | Outer connections are divided into left connections and right links. The specific definitions are as follows. Left join: Contains all records in the left table even if there are no matching records in the right table mysql> select * from user left join order_goods on user.uid = order_goods.uid; +-----+-----------+------------+------+ ------+---------------+-----------+ | uid | username | password | oid | uid | name | buytime | +-----+-----------+------------+------+----- -+---------------+----------+ | 10 | Gao Xiaofeng | 3124qwqw | 1 | 10 | Apple Mouse | 1212313 | | 3 | Wang Baoqiang | 1235531 | 2 | 3 | iphone 12s | 123121241 | | 12 | Li Xiaochao | 311aqqee | 3 | 12 | Sprite | 13232 333 | | 15 | Tong Xiaogang | 3cxvdfs | 4 | 15 | 34242123 | | 3 | Wang Baoqiang | 1235531 | 5 | 3 | iphone keyboard | 12123413 | | 1 | Jing Tian | 123456 | NULL | NULL ULL 二 | 245667 | NULL | NULL | NULL | 4 | Jing Boran | 123455 | NULL | NULL | NULL | 5 | Fan Bingbing | 5ab cwa | NULL | NULL | NULL | | NULL | | 6 | 黄晓明 | abcdeef | NULL | NULL | NULL | 7 | anglebaby | caption | NULL | NULL | NULL | 8 | TFBOYS abcd | www | NULL | NULL | NULL | NULL | | 9 | An Xiaochao | 12tfddwd | NULL | NULL | NULL | 3 | Han Xiaoping | 121rcfwrfq | NULL | NULL | NULL | | NULL | | 14 | Song Xiaokang | 123123tcsd | NULL | NULL | NULL | ------------+------+------+---------------+------- ----+ 16 rows in set (0.00 sec)
Right join: contains all the records in the right table even the records that do not match it in the right table Category | Detailed explanation |
---|
Basic syntax | select table 1.field [as alias], table n.field from table 1 right JOIN table n on condition; | Example | select * from user right join order_goods on user.uid = order_goods.uid; | Example description | Query which users in the product table have purchased products and display the user information | ##mysql> select * from user right join order_goods on user.uid = order_goods.uid;+------+-----------+----------+-----+ -----+---------------+----------+ | uid | username | password | oid | uid | name | buytime | +------+-----------+----------+-----+-----+-- -------------+----------+ | 10 | Gao Xiaofeng | 3124qwqw | 1 | 10 | Apple Mouse | 1212313 | | 3 | Wang Baoqiang | 1235531 | 2 | 3 | iphone 12s | 123121241 | | 12 | Li Xiaochao | 311aqqee | 3 | 12 | Sprite | 13232333 | | 15 | Tong Xiaogang | 3cxvdfs | 4 | 15 | 34242123 | | 3 | Wang Baoqiang | 1235531 | 5 | 3 | iphone keyboard | 12123413 | +-------+----------+-------- --+-----+-----+---------------+-----------+ 5 rows in set (0.00 sec)
SubquerySometimes, when we query, the required condition is the result of another select statement, then we need to use a subquery. Keywords used for subqueries include in, not in, =, !=, exists, not exists, etc. Category | Detailed explanation | Basic syntax | select field from table where field in (condition) | Example 1 | select * from user where uid in (1,3,4); | Example 1 Description | Query the specified user according to id | Example 2 | select * from user where uid in ( select uid from order_goods); | Example 2 Description | Display user information that has purchased goods | ##Example 1: mysql> select * from user where uid in (1,3,4);
+-----+-----------+----------+
| uid | username | password |
+-----+-----------+----------+
| 1 | Jing Tian | 123456 |
| 3 | Wang Baoqiang | 1235531 |
| 4 | Jing Boran | 123455 |
+-----+-----------+----------+
3 rows in set (0.00 sec)
Example 2: mysql> select * from user where uid in (select uid from order_goods) ;
+-----+-----------+----------+
| uid | username | password |
+-----+-----------+----------+
| 10 | Gao Xiaofeng | 3124qwqw |
| 3 | Wang Baoqiang | 1235531 |
| 12 | Li Xiaochao | 311aqqee |
| 15 | Tong Xiaogang | 3cxvdfs |
+-----+-----------+----------+
4 rows in set (0.00 sec) ##mysql> select * from emp where deptno in (select deptno from dept);Record unionUse union and The union all keyword is used to query the data from two tables according to certain query conditions, and then merge the results and display them together. The main difference between the two is that the results are directly merged together, while union is the result of performing a distinct operation on the results after union all, and removing duplicate records. Category | Detailed explanation | Basic syntax | select statement 1 union[all] select statement 2 | Example | select * from user where uid in (1,3,4); | Example description | Combine the results of user information in the product table and user information in the user table |
mysql> select uid from user union select uid from order_goods;
+-----+
| uid |
+-----+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
| 10 |
| 11 |
| 12 |
| 13 |
| 14 |
| 15 |
+-----+
15 rows in set (0.00 sec)
Next Section<?php
echo "Hello Mysql";
?> - Chapter1Why choose this course to learn PHP
- Why learn PHP?
- What is PHP
- You can learn even with z...
- Why can't some people lea...
- Chapter2PHP environment installation
- What is the development e...
- Windows environment insta...
- Linux environment install...
- Other development environ...
- Tool selection for writin...
- Chapter3php basic syntax
- PHP basic syntax
- Our first piece of PHP co...
- Variables in php - you wi...
- echo display command
- Learning php annotations
- Data types are not myster...
- PHP integer type is an in...
- PHP data type Boolean (ac...
- PHP data type string
- PHP data type floating po...
- PHP flow control if else ...
- PHP data type NULL type
- php data type array
- Resource type of php data...
- PHP data type viewing and...
- Automatic conversion and ...
- Object (will learn later)
- PHP constants and variabl...
- PHP constants and variabl...
- PHP constants and variabl...
- PHP constants and variabl...
- Variable references for P...
- PHP basic syntax arithmet...
- PHP basic syntax assignme...
- PHP basic syntax: self-in...
- PHP basic syntax comparis...
- Logical operations of php...
- PHP basic syntax bit oper...
- PHP basic syntax: ternary...
- Chapter4PHP process control
- Process control in PHP
- PHP process control if co...
- PHP flow control if state...
- Nested if...else...elseif...
- Multiple nesting of if st...
- Use of branch structure s...
- Use of loop statements in...
- while loop
- The difference between do...
- PHP flow control for loop...
- PHP flow control goto syn...
- Chapter5Basic function syntax of PHP
- Basic function syntax of ...
- PHP function basic syntax...
- PHP custom function callb...
- PHP custom function varia...
- PHP custom function anony...
- Internal function of php ...
- Variable scope of php cus...
- Reference to parameters o...
- PHP custom function recur...
- Static variables of php c...
- php uses system built-in ...
- php file contains functio...
- PHP math commonly used fu...
- PHP function to obtain pe...
- php date validation funct...
- PHP gets localized timest...
- PHP program execution tim...
- PHP string common functio...
- Chapter6PHP arrays and data structures
- PHP arrays and data struc...
- php array definition
- PHP array calculation
- php for loop traverses in...
- php foreach traverses as...
- PHP list, each function t...
- PHP commonly used array m...
- Common functions for php ...
- Chapter7Regular expressions in PHP
- Regular expressions in PH...
- Delimiter expressed by ph...
- Atoms in php regular expr...
- Metacharacters in php reg...
- Pattern modifiers in php ...
- Tips and commonly used re...
- PHP uses regular expressi...
- Chapter8php file system
- File system
- php read file
- php creates and modifies ...
- php creates temporary fil...
- php move, copy and delete...
- php detect file attribute...
- Common functions and cons...
- php file locking mechanis...
- php directory processing ...
- php file permission setti...
- php file path function
- PHP implements file guest...
- PHP implementation exampl...
- Chapter9PHP file upload
- PHP file upload
- When uploading files, you...
- Steps to upload php files
- Precautions for php file ...
- php completes file upload...
- php multiple file upload
- PHP file upload progress ...
- Chapter10PHP image processing
- PHP image processing
- PHP image processing gd2 ...
- PHP uses image processing...
- PHP development verificat...
- php image scaling and cro...
- PHP image watermark proce...
- Chapter11PHP error handling
- Error handling
- PHP error handling prohib...
- PHP error handling error ...
- PHP error handling error ...
- PHP error handling custom...
- Chapter12Getting started with MySQL
- Getting Started with MySQ...
- Mysql database introducti...
- Mysql entertainment expla...
- mysql database installati...
- Data statement operation ...
- Mysql connect to database
- Mysql database operation
- Mysql data table operatio...
- Mysql data field operatio...
- Mysql data type
- Mysql character set
- Mysql table engine
- Mysql index
- Mysql add, delete, modify...
- Mysql add, delete, modify...
- Mysql multi-table joint q...
- Mysql addition, deletion,...
- Mysql add, delete, modify...
- DCL statement
- Learn commonly used Engli...
- Chapter13PHP operates mysql database
- PHP operates mysql databa...
- PHP database connection s...
- PHP operates the database...
- PHP database operation: m...
- PHP database operation: p...
- PHP database operation: b...
- PHP database operation to...
- The ultimate solution to ...
- Chapter14php session management and control
- session overview
- Overview of Cookies for P...
- php session control Cooki...
- PHP session control using...
- php SESSION application e...
- Session management and co...
- Chapter15Making a thief program through cURL
- php curl usage methods an...
- php curl custom get metho...
- php curl uses post to sen...
- Making a thief program th...
- Chapter16Learn commonly used English words in PHP
- List of commonly used Eng...
| |