在做报表的时候,遇到这么一个问题,由于数据量大,数据上传时间问题,经常要处理更新几天前的数据,这时需要涉及UPDATE或者INSERT两个操作,这时推荐用MERGE INTO,但用这个时候需注意一个问题,我先来一个测试: SQL select * from ytrep.tab1; COL_A COL_B
在做报表的时候,遇到这么一个问题,由于数据量大,数据上传时间问题,经常要处理更新几天前的数据,这时需要涉及UPDATE或者INSERT两个操作,这时推荐用MERGE INTO,但用这个时候需注意一个问题,我先来一个测试:
SQL> select * from ytrep.tab1;
COL_A COL_B COL_C
---------- ---------- ----------
1 A
2 B
3 C
4 D
5 E
6 F
7 G
8 H
8 rows selected
SQL> select * from ytrep.tab2;
COL_A COL_B COL_C COL_D
---------- ---------- ---------- ----------
1 A A1 1
1 A A1 2
2 B B1 1
3 C C1 1
4 D D1 1
5 E E1 2
5 E E2 1
6 F F1 1
6 F F1 2
9 G G1 1
10 H H1 1
11 rows selected
有以上两表,其中需要根据ytrep.tab2表的col_a及col_b值来更新ytrep.tab1的col_c值,这时,我们来执行以下SQL:
Merge into ytrep.tab1 p
using (
select t.col_A, t.col_B, col_C
from ytrep.tab2 t
) tmp
on (p.col_A= tmp.col_A and p.col_B= tmp.col_B)
when not matched then
insert (col_A, col_B, col_C) values (tmp.col_a, tmp.col_B, tmp.col_C)
when matched then
update set p.col_c = tmp.col_c;
提示:
ORA-30926: 无法在源表中获得一组稳定的行
ORA-30926: unable to get a stable set of rows in the source tables
好了,现在我们来分析为什么会出现这种错误,在ON条件里,我们可以看到p.col_A= tmp.col_A and p.col_B= tmp.col_B,
当tmp表返回记录后,ytrep.tab1取第一条记录 col_a = '1' and col_b = 'A' 与tmp表关联时,发现tmp有两条匹配的记录,这时ORACLE就无法判断到底UPDATE SET时应该取TMP表的哪条记录的COL_C值是A1还是A2了,这时ORACLE就返回以上错误;
这时,我们就可以推断,TMP结果集里返回的在ON条件里有涉及字段的值必须是唯一的,否则肯定报以上ORA-30926错误;
这时,有人也许会问,那到底该怎么写呢?
可分为两种情况:
1、一种是通过将TMP表的数据GROUP BY,取得MIN或MAX值;
Merge into ytrep.tab1 p
using (
select t.col_A, t.col_B, max(col_C) col_C --min(col_C) col_C
from ytrep.tab2 t
group by t.col_a, t.col_b
) tmp
on (p.col_A= tmp.col_A and p.col_B= tmp.col_B)
when not matched then
insert (col_A, col_B, col_C) values (tmp.col_a, tmp.col_B, tmp.col_C)
when matched then
update set p.col_c = tmp.col_c;
SQL> select * from ytrep.tab1;
COL_A COL_B COL_C
---------- ---------- ----------
1 A A1
2 B B1
3 C C1
4 D D1
5 E E2
6 F F1
7 G
8 H
10 H H1
9 G G1
10 rows selected
2、一种是分组后再根据某列值排序取第一行或最后一行值;
Merge into ytrep.tab1 p
using (
select distinct t.col_A, t.col_B, first_value(col_c) over (partition by t.col_a, t.col_b order by t.col_d asc) col_c
from ytrep.tab2 t
) tmp
on (p.col_A= tmp.col_A and p.col_B= tmp.col_B)
when not matched then
insert (col_A, col_B, col_C) values (tmp.col_a, tmp.col_B, tmp.col_C)
when matched then
update set p.col_c = tmp.col_c;
SQL> select * from ytrep.tab1;
COL_A COL_B COL_C
---------- ---------- ----------
1 A A1
2 B B1
3 C C1
4 D D1
5 E E2
6 F F1
7 G
8 H
10 H H1
9 G G1
10 rows selected
以上两个SQL,大家可以通过改变order by排序方式对比一下结果。
以上只是本人学习测试结果,如有不同意见或或好的建议,可以留言。谢谢!

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

ToaddauserremotelytoMySQL,followthesesteps:1)ConnecttoMySQLasroot,2)Createanewuserwithremoteaccess,3)Grantnecessaryprivileges,and4)Flushprivileges.BecautiousofsecurityrisksbylimitingprivilegesandaccesstospecificIPs,ensuringstrongpasswords,andmonitori

TostorestringsefficientlyinMySQL,choosetherightdatatypebasedonyourneeds:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseTEXTforlong-formtextcontent.4)UseBLOBforbinarydatalikeimages.Considerstorageov

When selecting MySQL's BLOB and TEXT data types, BLOB is suitable for storing binary data, and TEXT is suitable for storing text data. 1) BLOB is suitable for binary data such as pictures and audio, 2) TEXT is suitable for text data such as articles and comments. When choosing, data properties and performance optimization must be considered.

No,youshouldnotusetherootuserinMySQLforyourproduct.Instead,createspecificuserswithlimitedprivilegestoenhancesecurityandperformance:1)Createanewuserwithastrongpassword,2)Grantonlynecessarypermissionstothisuser,3)Regularlyreviewandupdateuserpermissions

MySQLstringdatatypesshouldbechosenbasedondatacharacteristicsandusecases:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseBINARYorVARBINARYforbinarydatalikecryptographickeys.4)UseBLOBorTEXTforlargeuns


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

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.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1
Easy-to-use and free code editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
