1242 Subquery returns more than 1 row solution

1. Description of the situation

Execute the following statement:

update corp_stockholder_info a set a.STOCKHOLDER_NAME =(select CORP_NAME from corp_info c where c.CORP_KEY=a.STOCKHOLDER_CORP_KEY) 

1242 Subquery returns more than 1 row error occurs

2. Error description

I encountered an error in MySQL where a subquery returned multiple rows.
This error occurs when a subquery returns multiple rows, but the SQL statement only expects one row.

select CORP_NAME from corp_info c where c.CORP_KEY=a.STOCKHOLDER_CORP_KEY

Find out that one CORP_KEY corresponds to multiple CORP_NAME

3. Error reporting and resolution

方法1. select CORP_KEY,count(*) from corp_info group by CORP_KEY having count(CORP_KEY)>1;

The above sql statement finds out which CORP_KEY corresponds to multiple CORP_NAME and manually modify it.

Method 2. Use aggregate functions, such as MAX() or MIN(), to return a single value from the subquery.

Guess you like

Origin blog.csdn.net/m0_58823014/article/details/130640710