Home > Database > Mysql Tutorial > mysql把一个表某个字段的内容复制到另一张表的某个字段的SQL语句_MySQL

mysql把一个表某个字段的内容复制到另一张表的某个字段的SQL语句_MySQL

WBOY
Release: 2016-06-01 13:17:29
Original
1101 people have browsed it


需求:把一个表某个字段内容复制到另一张表的某个字段。

实现sql语句1:

UPDATE file_manager_folder f1
LEFT OUTER JOIN file_manager_folder f2
    ON f1.name = f2.name AND f2.parentId = 54
SET f1.parentId = 54
WHERE f2.name IS NULL AND f1.id IN (1,2,3);

实现sql语句2:
update B set extra = A.extra from A join B on (A.id = B.id);

实现sql语句3:
update b set b.sms = (select a.sms from a where a.id = b.id)
需要确定两张表中的id都是主键或者唯一

实现sql语句4:


UPDATE A SET A.SMS = (SELECT B.SMS FROM B WHERE A.ID = B.ID) WHERE EXISTS (SELECT 1 FROM B WHERE A.ID = B.ID);

实现sql语句5:
复制一个表字段数据到另外一个表的字段,可以这么写:
实现sql语句5:

UPDATE tb_1 INNER JOIN tb_2 ON tb_1.tid = tb_2.tid
SET tb_1.tcontent = tb_2.tcontent


附:同表复制

需求:把同一张表的一个字段内的内容复制到另一个字段里

例1:
我想把article表中A字段的内容复制到article表中B字段里面sql语句为:
update article set B=A;

例2:
有时候,我们需要复制某个字段一整列的数据到另外一个新的字段中,这很简单,SQL可以这么写:
UPDATE tb_1 SET content_target = content_source;
大概写法如下:
Update {your_table} set {source_field} = {object_field} WHERE cause

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template