The difference between Update and select in MySQL for single and multiple tables, and views and temporary tables

The difference between Update and select in MySQL for single and multiple tables, and views and temporary tables

1. Use data from table A to update the content of table B in MySQL

For example: you want to update some column attributes in the data table, but the content of the modified attributes comes from the chanpin table. The select keyword should not appear in SQL language

update data d,chanpin c set d.zhulei=c.zhulei,d.xiaolei=c.xiaolei,d.fenxiang=c.fenxiang,d.zhuanye=c.zhuanye,d.jiliang=c.jiliang,d.gs=c.zgs,d.xzgs=c.zgs,d.bjgs=(c.zgs*d.jdxs*d.jishu*d.xs1*d.xs2*d.xs3),d.wygs=d.bjgs,d.hzgs=? where d.id=? and c.chanpin=? and c.fenlei=?";

2. The table for update and select in MySQL is the same table

What is a bit annoying about MySQL is that its select from statement cannot be followed by the name of the table to be updated, for example:

update TBCP_SELLER set status =( select STATUS from TBCP_SELLER where tbid=2011645303 ) where tbid=2011645303

The table to be updated has the same name as the table to be queried. When SQL is executed, an error message is displayed: You can't specify target table 'TBCP SELLER' for update in FROM clause
To deal with this problem, we can construct a view and use nested select to implement query and update, as shown below:

update TBCP_SELLER set status =( select STATUS from ( select * from TBCP_SELLER )as x where tbid=2011645303 ) where tbid=2011645303

Note the following in the SQL above:

( select * from TBCP_SELLER )as x

as x is required, otherwise an error will be reported: Every derived table must have its own alias (every derived table must have its own alias)

3. The difference between temporary tables and views

Temporary Tables

A temporary table is a table created in the system temporary folder. If used properly, it can be operated in various ways like a normal table and will be automatically released when VFP exits. Creating a MySQL temporary table is easy. Add the TEMPORARY keyword to the normal CREATE TABLE statement. Sql code:

CREATE TEMPORARY TABLE tmp_table ( name VARCHAR(10) NOT NULL, value INTEGER NOT NULL )

CREATE TEMPORARY TABLE tmp_table ( name VARCHAR(10) NOT NULL, value INTEGER NOT NULL )

view

From a user's perspective, a view is a look at the data in the database from a specific perspective.

From the perspective of the database system, a view is a virtual table defined by a query consisting of SELECT statements.

From the perspective of the database system, a view is composed of data from one or more tables.

From the outside of the database system, a view is just like a table. All general operations that can be performed on a table can be applied to a view, such as query, insert, modify, delete, etc.

You may also be interested in:
  • A brief discussion on MySQL select optimization solution
  • MySQL select results to perform update example tutorial
  • Solve the problem that MySQL read-write separation causes data not to be selected after insert
  • How MySQL Select Statement is Executed
  • Implementation of MySQL select in subquery optimization
  • MySQL learning notes: complete select statement usage example detailed explanation
  • MySQL select, insert, update batch operation statement code examples
  • A brief understanding of MySQL SELECT execution order
  • Explanation of mysql transaction select for update and data consistency processing
  • Detailed explanation of the use of MySQL select cache mechanism
  • Summary of Select usage in MySql database
  • How a select statement is executed in MySQL

<<:  How to use Vue to develop public account web pages

>>:  Solutions for building ping and nfs in embedded Linux development environment

Recommend

js to achieve cool fireworks effect

This article shares the specific code for using j...

What are the attributes of the JSscript tag

What are the attributes of the JS script tag: cha...

Nginx implements https website configuration code example

https base port 443. It is used for something cal...

Analysis of the principles and usage of Docker container data volumes

What is a container data volume If the data is in...

In-depth understanding of Linux load balancing LVS

Table of contents 1. LVS load balancing 2. Basic ...

vue dynamic component

Table of contents 1. Component 2. keep-alive 2.1 ...

Detailed explanation of the EXPLAIN command and its usage in MySQL

1. Scenario description: My colleague taught me h...

How to import, register and use components in batches in Vue

Preface Components are something we use very ofte...

Implementation steps for building a local web server on Centos8

1 Overview System centos8, use httpd to build a l...

mysql wildcard (sql advanced filtering)

Table of contents First, let's briefly introd...

How to run tomcat source code in maven mode

Preface Recently, I was analyzing the startup pro...

Example of creating table statements for user Scott in MySQL version of Oracle

Overview: Oracle scott user has four tables, whic...

How to create a MySQL database and support Chinese characters

Let's first look at the MySQL official docume...

Solution to large line spacing (5 pixels more in IE)

Copy code The code is as follows: li {width:300px...

Detailed explanation of common usage of pseudo-classes before and after in CSS3

The before/after pseudo-class is equivalent to in...