search
HomeDatabaseMysql Tutorialjoin update vs sub-selects update

开发说使用多表关联进行更新的时候发现没有正确的更新记录(事后发现是条件问题),之前一直没怎么使用关联更新,这次看了下,发现了个问题,当返回多行记录的时候并不会像传统的子查询更新那样报错,而是随机选择一个记录进行更新( 貌似最后一个? ) 因此

开发说使用多表关联进行更新的时候发现没有正确的更新记录(事后发现是条件问题),之前一直没怎么使用关联更新,这次看了下,发现了个问题,当返回多行记录的时候并不会像传统的子查询更新那样报错,而是随机选择一个记录进行更新(貌似最后一个?
因此,虽然关联更新会快很多,但是要考虑的这个不确定带来的隐患。

gtlions=# create table joinupdate1(id int,name varchar(20));
NOTICE:  Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'id' as the Greenplum Database data distribution key for this table.
HINT:  The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
CREATE TABLE
gtlions=# create table joinupdate2(id int,name varchar(20));
NOTICE:  Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'id' as the Greenplum Database data distribution key for this table.
HINT:  The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
CREATE TABLE
gtlions=# insert into  joinupdate1 values(1,'a');
INSERT 0 1
gtlions=# insert into  joinupdate1 values(2,'b');
INSERT 0 1
gtlions=# insert into  joinupdate2 values(1,'b');
INSERT 0 1
gtlions=# insert into  joinupdate2 values(2,'b');
INSERT 0 1
gtlions=# truncate table joinupdate2;
TRUNCATE TABLE
gtlions=# insert into  joinupdate2 values(1,'c');
INSERT 0 1
gtlions=# insert into  joinupdate2 values(2,'d'); 
INSERT 0 1
gtlions=# select * from joinupdate1;
 id | name 
----+------
  1 | a
  2 | b
(2 rows)

gtlions=# select * from joinupdate2;
 id | name 
----+------
  1 | c
  2 | d
(2 rows)

gtlions=# begin;
BEGIN
gtlions=# update joinupdate1 set name=(select name from joinupdate2 where joinupdate1.id=joinupdate2.id);
UPDATE 2
gtlions=# select * from joinupdate1;
 id | name 
----+------
  1 | c
  2 | d
(2 rows)

gtlions=# rollback;
ROLLBACK
gtlions=# end;
WARNING:  there is no transaction in progress
COMMIT
gtlions=# select * from joinupdate1;
 id | name 
----+------
  1 | a
  2 | b
(2 rows)

gtlions=# begin;
BEGIN
gtlions=# update joinupdate1 set name=joinupdate2.name from joinupdate2 where joinupdate1.id=joinupdate2.id;
UPDATE 2
gtlions=# select * from joinupdate1;
 id | name 
----+------
  1 | c
  2 | d
(2 rows)

gtlions=# rollback;
ROLLBACK
gtlions=# insert into joinupdate2 values(1,'e');
INSERT 0 1
gtlions=# begin;
BEGIN
gtlions=# update joinupdate1 set name=(select name from joinupdate2 where joinupdate1.id=joinupdate2.id);
ERROR:  more than one row returned by a subquery used as an expression  (seg0 slice2 h1:40000 pid=14123)
gtlions=# rollback;
ROLLBACK
gtlions=# begin;
BEGIN
gtlions=# update joinupdate1 set name=joinupdate2.name from joinupdate2 where joinupdate1.id=joinupdate2.id;
UPDATE 2
gtlions=# select * from joinupdate1;
 id | name 
----+------
  1 | e
  2 | d
(2 rows)

gtlions=# update joinupdate1 set name=joinupdate2.name from joinupdate2 where joinupdate1.id=joinupdate2.id;
UPDATE 2
gtlions=# select * from joinupdate1;
 id | name 
----+------
  1 | e
  2 | d
(2 rows)

gtlions=# update joinupdate1 set name=joinupdate2.name from joinupdate2 where joinupdate1.id=joinupdate2.id;
UPDATE 2
gtlions=# select * from joinupdate1;
 id | name 
----+------
  1 | e
  2 | d
(2 rows)

gtlions=# insert into joinupdate2 values(1,'f');
INSERT 0 1
gtlions=# update joinupdate1 set name=joinupdate2.name from joinupdate2 where joinupdate1.id=joinupdate2.id;
UPDATE 2
gtlions=# select * from joinupdate1;
 id | name 
----+------
  1 | f
  2 | d
(2 rows)

gtlions=# rollback;
ROLLBACK

-EOF-

Statement
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
修复:谷歌浏览器更新检查失败错误代码3:0x80040154修复:谷歌浏览器更新检查失败错误代码3:0x80040154Apr 13, 2023 pm 05:46 PM

谷歌浏览器是全球最受欢迎的浏览器之一,许多用户更喜欢将其用作 Windows PC 上的默认浏览器。Chrome 提供了广泛的功能,使浏览体验愉快而轻松,因此,它仍然是最受信任的浏览器之一。但是,就像任何其他浏览器一样,即使 Chrome 也有其自身的缺点,它同样容易在您最需要的时候出现错误和故障。一个这样的错误是错误代码 3:0x80040154,这发生在检查 Google Chrome 更新时。错误消息显示为“检查更新时发生错误。更新检查无法启动(错误代码 3:0x80080005)或(错误

MySql中如何使用JOINMySql中如何使用JOINJun 04, 2023 am 08:02 AM

JOIN的含义就如英文单词“join”一样,连接两张表,大致分为内连接,外连接,右连接,左连接,自然连接。先创建两个表,下面用于示例CREATETABLEt_blog(idINTPRIMARYKEYAUTO_INCREMENT,titleVARCHAR(50),typeIdINT);SELECT*FROMt_blog;+----+-------+--------+|id|title|typeId|+----+-------+--------+|1|aaa|1||2|bbb|2||3|ccc|3|

如何启用/禁用 VS Code 自动更新如何启用/禁用 VS Code 自动更新Apr 28, 2023 am 09:28 AM

如果您正在使用VisualStudioCode(VSCode)并考虑如何禁用它的自动软件更新以及如何禁用其扩展的自动更新,那么请阅读本文。如果你不经常使用VSCode,隔了很长一段时间打开编辑器并想启用自动更新,本文也将指导你这样做。让我们详细讨论启用或禁用VSCode自动更新的不同方法。目录方法一:使用设置启用/禁用VSCode自动更新第一步:打开VS代码,在左下角点击齿轮状的符号。第2步:在出现的列表中单击设置。第3步:在搜索栏中输入更新并回车。查找更新:模式第4

KDE Plasma 6.1 brings many enhancements to the popular Linux desktopKDE Plasma 6.1 brings many enhancements to the popular Linux desktopJun 23, 2024 am 07:54 AM

After several pre-releases, the KDE Plasma development team unveiled version 6.0 of its desktop environment for Linux and BSD systems on 28 February, using the Qt6 framework for the first time. KDE Plasma 6.1 now comes with a number of new features t

Microsoft compatibility telemetry占用高CPU的解决方法Microsoft compatibility telemetry占用高CPU的解决方法Mar 16, 2024 pm 10:16 PM

我们在使用win10系统的时候有时候会遇到电脑变得卡顿的情况,然后我们在查看后台进程的时候会发现一个Microsoftcompatibilitytelemetry的进程占用资源特别的高,那么这是怎么回事?用户们可以尝试卸载三方防护软件后尝试干净启动来进行操作,下面就让本站来为用户们来仔细的介绍一下Microsoftcompatibilitytelemetry占用高CPU的解决方法吧。Microsoftcompatibilitytelemetry占用高CPU的解决方法方法一:卸载三方防护软件后尝试

MySQL Join使用原理是什么MySQL Join使用原理是什么May 26, 2023 am 10:07 AM

Join的类型leftjoin,以左表为驱动表,以左表作为结果集基础,连接右表的数据补齐到结果集中rightjoin,以右表为驱动表,以右表作为结果集基础,连接左表的数据补齐到结果集中innerjoin,结果集取两个表的交集fulljoin,结果集取两个表的并集mysql没有fulljoin,union取代union与unionall的区别为,union会去重crossjoin笛卡尔积如果不使用where条件则结果集为两个关联表行的乘积与,的区别为,crossjoin建立结果集时会根据on条件过

SQL中UPDATE语句怎么用SQL中UPDATE语句怎么用Jun 02, 2023 pm 09:13 PM

SQLUPDATE语句Update语句用于修改表中的数据。语法如下:UPDATE表名称SET列名称=新值WHERE列名称=某值"Person"表:LastNameFirstNameAddressCityGatesBillXuanwumen10BeijingWilsonChamps-Elysees更新某一行中的一个列UPDATEPersonSETFirstName="Fred"WHERELastName="Wilson"结果:LastNa

Fitbit Ace LTE receives major update with new games, contactless payment and other featuresFitbit Ace LTE receives major update with new games, contactless payment and other featuresAug 08, 2024 pm 09:39 PM

The Fitbit Ace LTE was officially launched in May, but is currently only available in the US. The smartwatch is aimed specifically at children, who can receive rewards for games through a more active lifestyle, while parents can always monitor their

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool