Use join in update query instead of columns when updating rows
P粉170438285
2023-08-02 12:32:01
<p>更新之前(原始示例表):</p>
<table class="s-table">
<thead>
<tr>
<th>document_id</th>
<th>meta_key</th>
<th>meta_value</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>iban</td>
<td>IBAN123456</td>
</tr>
<tr>
<td>1</td>
<td>bankaccount</td>
<td>ACCT987654</td>
</tr>
<tr>
<td>2</td>
<td>iban</td>
<td>IBAN555555</td>
</tr>
<tr>
<td>2</td>
<td>bankaccount</td>
<td>ACCT444444</td>
</tr>
<tr>
<td>3</td>
<td>iban</td>
<td>IBAN888888</td>
</tr>
<tr>
<td>3</td>
<td>bankaccount</td>
<td>ACCT333333</td>
</tr>
</tbody>
</table>
<p>运行SQL更新查询后:</p>
<table class="s-table">
<thead>
<tr>
<th>document_id</th>
<th>meta_key</th>
<th>meta_value</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>iban</td>
<td>IBAN123456</td>
</tr>
<tr>
<td>1</td>
<td>bankaccount</td>
<td>IBAN123456</td>
</tr>
<tr>
<td>2</td>
<td>iban</td>
<td>IBAN555555</td>
</tr>
<tr>
<td>2</td>
<td>bankaccount</td>
<td>IBAN555555</td>
</tr>
<tr>
<td>3</td>
<td>iban</td>
<td>IBAN888888</td>
</tr>
<tr>
<td>3</td>
<td>bankaccount</td>
<td>IBAN888888</td>
</tr>
</tbody>
</table>
<p>我需要一个查询来实现上述表格的结果吗?</p>
document_id
,meta_key
, andmeta_value
. You want to update the meta_value of the row whose meta_key is bankaccount to the row whose meta_key is iban. The corresponding meta_value.The following is a SQL query to achieve this goal:
Before running any update queries, remember to back up your database and test it in a secure environment!