使用ROW_NUMBER()

时间:2015-06-01 18:38:46

标签: sql-update

我想更新ProductType为-1的ManufID,并将其替换为ProductType为1的ManufID。我有这个样本记录:

<table border="0" style="font-size:10; font-family: Lucida-Console; border-width: 4px; cell-spacing:0px; cell-padding:2px;">
	<thead><tr>
		<th style="font-weight:bold;background-color:LightSteelBlue;font-size:14;">AltID</th>
		<th style="font-weight:bold;background-color:LightSteelBlue;font-size:14;">ItemID</th>
		<th style="font-weight:bold;background-color:LightSteelBlue;font-size:14;">ManufID</th>
		<th style="font-weight:bold;background-color:LightSteelBlue;font-size:14;">MaxCount-Manufid</th>
		<th style="font-weight:bold;background-color:LightSteelBlue;font-size:14;">ProductType</th>
	</tr></thead>
	<tbody>
		<tr style="background-color:#DDDDDD;font-size:12;">
			<td style="font-family: Tahoma;font-size;10;">acadun</td>
			<td style="font-family: Tahoma;font-size;10;">acadcs</td>
			<td style="font-family: Tahoma;font-size;10;">241</td>
			<td style="font-family: Tahoma;font-size;10;">241</td>
			<td style="font-family: Tahoma;font-size;10;">-1</td>
		</tr>
		<tr style="background-color:White;font-size:12;">
			<td style="font-family: Tahoma;font-size;10;">acadun</td>
			<td style="font-family: Tahoma;font-size;10;">acadep</td>
			<td style="font-family: Tahoma;font-size;10;">241</td>
			<td style="font-family: Tahoma;font-size;10;">241</td>
			<td style="font-family: Tahoma;font-size;10;">-1</td>
		</tr>
		<tr style="background-color:#DDDDDD;font-size:12;">
			<td style="font-family: Tahoma;font-size;10;">acadun</td>
			<td style="font-family: Tahoma;font-size;10;">acadfj</td>
			<td style="font-family: Tahoma;font-size;10;">241</td>
			<td style="font-family: Tahoma;font-size;10;">241</td>
			<td style="font-family: Tahoma;font-size;10;">-1</td>
		</tr>
		<tr style="background-color:White;font-size:12;">
			<td style="font-family: Tahoma;font-size;10;">acadun</td>
			<td style="font-family: Tahoma;font-size;10;">acadmn</td>
			<td style="font-family: Tahoma;font-size;10;">241</td>
			<td style="font-family: Tahoma;font-size;10;">241</td>
			<td style="font-family: Tahoma;font-size;10;">-1</td>
		</tr>
		<tr style="background-color:#DDDDDD;font-size:12;">
			<td style="font-family: Tahoma;font-size;10;">acadun</td>
			<td style="font-family: Tahoma;font-size;10;">acadnk</td>
			<td style="font-family: Tahoma;font-size;10;">1213</td>
			<td style="font-family: Tahoma;font-size;10;">241</td>
			<td style="font-family: Tahoma;font-size;10;">1</td>
		</tr>
		<tr style="background-color:White;font-size:12;">
			<td style="font-family: Tahoma;font-size;10;">acadun</td>
			<td style="font-family: Tahoma;font-size;10;">acadun</td>
			<td style="font-family: Tahoma;font-size;10;">1213</td>
			<td style="font-family: Tahoma;font-size;10;">241</td>
			<td style="font-family: Tahoma;font-size;10;">1</td>
		</tr>
		<tr style="background-color:#DDDDDD;font-size:12;">
			<td style="font-family: Tahoma;font-size;10;">acadun</td>
			<td style="font-family: Tahoma;font-size;10;">dpacadfj</td>
			<td style="font-family: Tahoma;font-size;10;">241</td>
			<td style="font-family: Tahoma;font-size;10;">241</td>
			<td style="font-family: Tahoma;font-size;10;">-1</td>
		</tr>
		<tr style="background-color:White;font-size:12;">
			<td style="font-family: Tahoma;font-size;10;">acadun</td>
			<td style="font-family: Tahoma;font-size;10;">dpacadkd</td>
			<td style="font-family: Tahoma;font-size;10;">241</td>
			<td style="font-family: Tahoma;font-size;10;">241</td>
			<td style="font-family: Tahoma;font-size;10;">-1</td>
		</tr>
		<tr style="background-color:#DDDDDD;font-size:12;">
			<td style="font-family: Tahoma;font-size;10;">acadun</td>
			<td style="font-family: Tahoma;font-size;10;">dpacadnk</td>
			<td style="font-family: Tahoma;font-size;10;">241</td>
			<td style="font-family: Tahoma;font-size;10;">241</td>
			<td style="font-family: Tahoma;font-size;10;">-1</td>
		</tr>
		<tr style="background-color:White;font-size:12;">
			<td style="font-family: Tahoma;font-size;10;">acadun</td>
			<td style="font-family: Tahoma;font-size;10;">dpacadol</td>
			<td style="font-family: Tahoma;font-size;10;">241</td>
			<td style="font-family: Tahoma;font-size;10;">241</td>
			<td style="font-family: Tahoma;font-size;10;">-1</td>
		</tr>
		<tr style="background-color:#DDDDDD;font-size:12;">
			<td style="font-family: Tahoma;font-size;10;">acadun</td>
			<td style="font-family: Tahoma;font-size;10;">olacad</td>
			<td style="font-family: Tahoma;font-size;10;">241</td>
			<td style="font-family: Tahoma;font-size;10;">241</td>
			<td style="font-family: Tahoma;font-size;10;">-1</td>
		</tr>
	</tbody>
</table>

更新后,结果应如下所示:

<table border="0" style="font-size:10; font-family: Lucida-Console; border-width: 4px; cell-spacing:0px; cell-padding:2px;">
	<thead><tr>
		<th style="font-weight:bold;background-color:LightSteelBlue;font-size:14;">AltID</th>
		<th style="font-weight:bold;background-color:LightSteelBlue;font-size:14;">ItemID</th>
		<th style="font-weight:bold;background-color:LightSteelBlue;font-size:14;">ManufID</th>
		<th style="font-weight:bold;background-color:LightSteelBlue;font-size:14;">MaxCount-Manufid</th>
		<th style="font-weight:bold;background-color:LightSteelBlue;font-size:14;">ProductType</th>
	</tr></thead>
	<tbody>
		<tr style="background-color:#DDDDDD;font-size:12;">
			<td style="font-family: Tahoma;font-size;10;">acadun</td>
			<td style="font-family: Tahoma;font-size;10;">acadcs</td>
			<td style="font-family: Tahoma;font-size;10;">1213</td>
			<td style="font-family: Tahoma;font-size;10;">241</td>
			<td style="font-family: Tahoma;font-size;10;">-1</td>
		</tr>
		<tr style="background-color:White;font-size:12;">
			<td style="font-family: Tahoma;font-size;10;">acadun</td>
			<td style="font-family: Tahoma;font-size;10;">acadep</td>
			<td style="font-family: Tahoma;font-size;10;">1213</td>
			<td style="font-family: Tahoma;font-size;10;">241</td>
			<td style="font-family: Tahoma;font-size;10;">-1</td>
		</tr>
		<tr style="background-color:#DDDDDD;font-size:12;">
			<td style="font-family: Tahoma;font-size;10;">acadun</td>
			<td style="font-family: Tahoma;font-size;10;">acadfj</td>
			<td style="font-family: Tahoma;font-size;10;">1213</td>
			<td style="font-family: Tahoma;font-size;10;">241</td>
			<td style="font-family: Tahoma;font-size;10;">-1</td>
		</tr>
		<tr style="background-color:White;font-size:12;">
			<td style="font-family: Tahoma;font-size;10;">acadun</td>
			<td style="font-family: Tahoma;font-size;10;">acadmn</td>
			<td style="font-family: Tahoma;font-size;10;">1213</td>
			<td style="font-family: Tahoma;font-size;10;">241</td>
			<td style="font-family: Tahoma;font-size;10;">-1</td>
		</tr>
		<tr style="background-color:#DDDDDD;font-size:12;">
			<td style="font-family: Tahoma;font-size;10;">acadun</td>
			<td style="font-family: Tahoma;font-size;10;">acadnk</td>
			<td style="font-family: Tahoma;font-size;10;">1213</td>
			<td style="font-family: Tahoma;font-size;10;">241</td>
			<td style="font-family: Tahoma;font-size;10;">1</td>
		</tr>
		<tr style="background-color:White;font-size:12;">
			<td style="font-family: Tahoma;font-size;10;">acadun</td>
			<td style="font-family: Tahoma;font-size;10;">acadun</td>
			<td style="font-family: Tahoma;font-size;10;">1213</td>
			<td style="font-family: Tahoma;font-size;10;">241</td>
			<td style="font-family: Tahoma;font-size;10;">1</td>
		</tr>
		<tr style="background-color:#DDDDDD;font-size:12;">
			<td style="font-family: Tahoma;font-size;10;">acadun</td>
			<td style="font-family: Tahoma;font-size;10;">dpacadfj</td>
			<td style="font-family: Tahoma;font-size;10;">1213</td>
			<td style="font-family: Tahoma;font-size;10;">241</td>
			<td style="font-family: Tahoma;font-size;10;">-1</td>
		</tr>
		<tr style="background-color:White;font-size:12;">
			<td style="font-family: Tahoma;font-size;10;">acadun</td>
			<td style="font-family: Tahoma;font-size;10;">dpacadkd</td>
			<td style="font-family: Tahoma;font-size;10;">1213</td>
			<td style="font-family: Tahoma;font-size;10;">241</td>
			<td style="font-family: Tahoma;font-size;10;">-1</td>
		</tr>
		<tr style="background-color:#DDDDDD;font-size:12;">
			<td style="font-family: Tahoma;font-size;10;">acadun</td>
			<td style="font-family: Tahoma;font-size;10;">dpacadnk</td>
			<td style="font-family: Tahoma;font-size;10;">1213</td>
			<td style="font-family: Tahoma;font-size;10;">241</td>
			<td style="font-family: Tahoma;font-size;10;">-1</td>
		</tr>
		<tr style="background-color:White;font-size:12;">
			<td style="font-family: Tahoma;font-size;10;">acadun</td>
			<td style="font-family: Tahoma;font-size;10;">dpacadol</td>
			<td style="font-family: Tahoma;font-size;10;">1213</td>
			<td style="font-family: Tahoma;font-size;10;">241</td>
			<td style="font-family: Tahoma;font-size;10;">-1</td>
		</tr>
		<tr style="background-color:#DDDDDD;font-size:12;">
			<td style="font-family: Tahoma;font-size;10;">acadun</td>
			<td style="font-family: Tahoma;font-size;10;">olacad</td>
			<td style="font-family: Tahoma;font-size;10;">1213</td>
			<td style="font-family: Tahoma;font-size;10;">241</td>
			<td style="font-family: Tahoma;font-size;10;">-1</td>
		</tr>
	</tbody>
</table>

我正在考虑在更新记录时使用ROW_NUMBER,但我还不知道如何使用它: - )

1 个答案:

答案 0 :(得分:0)

这会将ManufID ProductType -1行的ManufID设置为ProductType 1的第一行UPDATE yourTable SET ManufID = (SELECT ManufID FROM yourTable WHERE ProductType = 1 LIMIT 1) WHERE ProductType = -1 {1}}:

ProductType

请注意,如果您有多个行1 ManufIDgetByid不同,则可能会出现问题。在这种情况下,您必须决定要使用哪个值。