A Brief Analysis of Subqueries and Advanced Applications in MySql Database

A Brief Analysis of Subqueries and Advanced Applications in MySql Database

Subquery in MySql database:

Subquery: nesting another select statement in a select query statement. Its main function is to serve as a query condition or determine the data source.

The code example is as follows:

Example 1. Query students older than the average age:

select * from students where age > (select avg(age) from students);

Example 2. Query all the class names of the students in the class:

select name from classes where id in (select cls_id from students where cls_id is not null);

Example 3. Find the oldest and tallest student:

select * from students where (age, height) = (select max(age), max(height) from students);

Advanced Applications of MySql:

1. Add the queried data to a new table:

Use a subquery to insert the query results as data into a new table. This is achieved through the keywords create table ... select .... The code is as follows:

create table table name (field name 1, type constraint, ...) select field name from table name where query condition

The execution process is to first execute the select statement to determine the data source through the where condition, and then insert the queried data into the newly created table.

Note: When using this method, if you want to add data to a specified field in the table, you need to give the found field an alias that is the same as the field name in the table.

2. Add the results of the query to the table:

Use a subquery to insert the query results into the table as data. This is done using the keywords insert into ... select .... The code is as follows:

insert into table name (field name 1,...) select field name 1,.. from table name where query condition

The execution process is to first execute the select statement to filter out the specified data through the where condition, and then execute the insert into statement to add data to the specified field name.

3. Use the connection to update the data of a field in the table:

Use the connection to update the field data in the table, through the keyword update ... join.. keyword implementation, code implementation:

update table1 join table2 on table1.field = table2.field set table1.field = table2.field

The execution process is to connect the two tables and then set the value of the field in Table 2 to the specified field in Table 1.

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of the principles and usage examples of MySQL join query, union query, and subquery
  • Detailed explanation of MySQL multi-table query examples [link query, subquery, etc.]
  • MySQL implements multi-table association statistics (subquery statistics) example
  • Detailed explanation of MySQL database--multi-table query--inner join, outer join, subquery, correlated subquery
  • Detailed explanation of MySQL subqueries (nested queries), join tables, and combined queries
  • Detailed explanation of MySQL subquery operation examples
  • MYSQL subquery and nested query optimization example analysis
  • MySQL database performance optimization subquery
  • Using subqueries in MySQL database
  • Detailed explanation of query examples within subqueries in MySql

<<:  Detailed tutorial on installing Anaconda3 on Ubuntu 18.04

>>:  Differences between ES6 inheritance and ES5 inheritance in js

Recommend

Three steps to solve the IE address bar ICON display problem

<br />This web page production skills tutori...

VM VirtualBox virtual machine mount shared folder

One environment Install VMware Tools on CentOS 7 ...

Detailed Introduction to the MySQL Keyword Distinct

Introduction to the usage of MySQL keyword Distin...

How to use Volume to transfer files between host and Docker container

I have previously written an article about file t...

Extract specific file paths in folders based on Linux commands

Recently, there is a need to automatically search...

An example of how to optimize a project after the Vue project is completed

Table of contents 1. Specify different packaging ...

An article teaches you JS function inheritance

Table of contents 1. Introduction: 2. Prototype c...

Teach you step by step to develop a brick-breaking game with vue3

Preface I wrote a few examples using vue3, and I ...

Implementation of React virtual list

Table of contents 1. Background 2. What is a virt...

JavaScript immediate execution function usage analysis

We know that in general, a function must be calle...

How to use the Linux nl command

1. Command Introduction nl (Number of Lines) adds...

Detailed explanation of routes configuration of Vue-Router

Table of contents introduce Object attributes in ...

Convert XHTML CSS pages to printer pages

In the past, creating a printer-friendly version ...

How to create a flame effect using CSS

The main text starts below. 123WORDPRESS.COM Down...