我正在使用 C# 并且对 SQLite 很陌生。
我需要通过将记录字段“Value1”除以特定用户和日期范围的给定分母来更新记录字段(第一个记录日期到特定日期,例如 2012-03-01)。
所以我需要遍历数据集,如果 User=John 和 Date <= than 2012-03-01 然后将 Value1 除以 10
这显然不能单独在 SQL 中完成,因为我需要知道 Value1 是什么,以便我可以对其进行划分。我该怎么做呢?
下面是我需要修改哪些数据的示例
User | Date | Value1 | Value2 | Value3
----------------------------------------------
John | 2012-01-01 | *50 | 5 | 80
John | 2012-02-01 | *100 | 78 | 60
John | 2012-03-01 | *10 | 100 | 5
John | 2012-04-01 | 5 | 89 | 55
John | 2012-05-01 | 15 | 50 | 44
Max | 2012-01-01 | 40 | 25 | 90
Max | 2012-02-01 | 99 | 79 | 30
Max | 2012-03-01 | 100 | 110 | 59
Max | 2012-04-01 | 25 | 29 | 85
Max | 2012-05-01 | 15 | 30 | 88
修改后的数据除以 10
User | Date | Value1 | Value2 | Value3
----------------------------------------------
John | 2012-01-01 | *5 | 5 | 80
John | 2012-02-01 | *10 | 78 | 60
John | 2012-03-01 | *1 | 100 | 5
John | 2012-04-01 | 5 | 89 | 55
John | 2012-05-01 | 15 | 50 | 44
Max | 2012-01-01 | 40 | 25 | 90
Max | 2012-02-01 | 99 | 79 | 30
Max | 2012-03-01 | 100 | 110 | 59
Max | 2012-04-01 | 25 | 29 | 85
Max | 2012-05-01 | 15 | 30 | 88
如果 Value1 是常量,那么我可以使用下面的代码。但它不是,它会将 2012-01-01 更新为 2012-03-01 Value1 都将是 5。
Value1 = 50
Val = Value1 / 10
sql_cmd = sql_con.CreateCommand();
sql_cmd.CommandText = "UPDATE Tablename SET Value1= '"+Val+"' WHERE User = 'John' and Date <= '2012-03-01'";
sql_cmd.ExecuteNonQuery();