我正在向现有表添加新的int
列。我正在尝试为表中的所有行设置一个值。
players_table:
- id
- company_id
- rank // new column
要设置等级的初始值,我只想按公司分组将等级增加1。
示例
companies:
id | name
---------------
1 | first comp
2 | second comp
players:
id | company_id | rank
-----------------------
1 | 1 | 1
2 | 1 | 2
3 | 1 | 3
4 | 2 | 1
5 | 2 | 2
是否可以使用SQL来实现?
答案 0 :(得分:1)
您可以使用public static class GlobalClass
{
public static string myGlobal="";
}
class myClass
{
string myClassVariable = "";
private void method()
{
//myGlobal is accessible using this
GlobalClass.myGlobal ="some value";
//myClassVariable is accessible here
myClassVariable = "somevalue";
if(condition)
{
//myClassVariable is also accessible here
myClassVariable = "somevalue";
string ifBlockVariable = "";
}
//ifBlockVariable is not accessible here
}
}
:
row_number()
要设置值,请使用select p.*, row_number() over (partition by p.company_id order by p.id) as rank
from players p;
:
update
答案 1 :(得分:1)
您可以尝试使用ROW_NUMBER
或RANK
窗口功能。
SELECT *,ROW_NUMBER() OVER(PARTITION BY company_id order by id )rank
FROM players