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

Comprehensive understanding of HTML Form elements

As shown below: XML/HTML CodeCopy content to clip...

jQuery to achieve sliding stairs effect

This article shares the specific code of jQuery t...

A quick solution to the problem of PC and mobile adaptation

When making a web page, we usually need to consid...

jQuery custom magnifying glass effect

This article example shares the specific code of ...

5 Ways to Clear or Delete Large File Contents in Linux

Sometimes, while working with files in the Linux ...

Complete steps for using Echarts and sub-packaging in WeChat Mini Program

Preface Although the holiday is over, it shows up...

MySQL explain obtains query instruction information principle and example

explain is used to obtain query execution plan in...

Two ways to export csv in win10 mysql

There are two ways to export csv in win10. The fi...

Tips on MySQL query cache

Table of contents Preface Introduction to QueryCa...

Comprehensive analysis of isolation levels in MySQL

When the database concurrently adds, deletes, and...

Summary of MySQL database like statement wildcard fuzzy query

MySQL error: Parameter index out of range (1 >...

Detailed explanation of Angular structural directive modules and styles

Table of contents 1. Structural instructions Modu...

Use pure CSS to achieve switch effect

First is the idea We use the <input type="...

Mysql solves the database N+1 query problem

Introduction In orm frameworks, such as hibernate...

Detailed examples of ajax usage in js and jQuery

Table of contents Native JS How to send a get req...