我需要sql用于以下场景。
我有一些选定的行。
例如, 字段表,我已经过滤了以下值
Id | yearId
1 | 1
1 | 2
1 | 3
1 | 4
1 | 5
过滤后,我正在使用现有表进行内连接,
考虑具有以下列的
字段表Id,yearid,value1,value2,value3, VALUE4,NextYearValue,PreviousYearValue
我需要对表进行内部联接,并且应该像下面那样更新,
value2 = value2 + value1
value3 = value2 + value3
value4 = value3 + value2
NextYearValue = Value2 + value3 + value4
PreviousYearValue = NextYearValue (should be taken from the previous year value)
在计算value2 + value1 之后,value3应该得到更新的值2
value4应在完成计算后获得更新的value3和value2 NextYearValue应该获得所有udpated值。
PreviousYearValue应获取上一年的更新值 value(这意味着我认为这个场景中的上一行)
答案 0 :(得分:2)
以与第一个值相同的方式设置字段或执行2次更新。
$firstname="";
$surname ="";
$id ="";
$faculty_of_study= "";
$enrolled_course ="";
$gender ="";
$password= "";
$telephone_no="";
$email_address="";
$sql="";
$firstname = addslashes($_POST["firstname"]);
$surname = addslashes($_POST["surname"]);
$id = addslashes($_POST["id"]);
$faculty_of_study = addslashes($_POST["faculty_of_study"]);
$enrolled_course = addslashes($_POST["enrolled_course"]);
$gender = addslashes($_POST["gender"]);
$password = addslashes($_POST["password"]);
$telephone_no = addslashes($_POST["telephone_no"]);
$email_address = addslashes($_POST["email_address"]);
$sql = "INSERT INTO people (firstname, surname, id,faculty_of_study, enrolled_course, gender, password, telephone_no, email_address); VALUES ("$firstname", "$surname", "$id", "$faculty_of_study", "$enrolled_course", "$gender","$password", "$telephone_no", "$email_address")";
这基本上就是你想做的事情吗?
如果由于某种原因你不能这样做,你可以这样做:
Update @Tbl
set value1=value1+value1,
value2=value1+value1
虽然那是2次打电话,但这不是我要做的事情
如果由于某种原因这不令人满意,也许您可以详细说明您的情况?因为2列具有相同的值是很奇怪的。
答案 1 :(得分:1)
我提出了两种方法,我不知道哪种方法更快。
由于您的计算并不复杂,您可以进行数学运算:
UPDATE FieldTable SET
value2 = value1 + value2
value3 = value1 + 2*value2 + value3
value4 = 2*value1 + 4*value2 + 2*value3
NextYearValue = 4*value1 + 7*value2 + 3*value3
PreviousYearValue = NextYearValue
在 MySql 方法中,您只需使用变量:
UPDATE FieldTable
JOIN
(
SELECT
id,
@uvalue2 :=value2 + value3 uvalue2,
@uvalue3 :=@uvalue2 + value3 uvalue3,
@uvalue4 :=@uvalue2 + @uvalue3 uvalue4,
@uvalue2 + @uvalue3 + @uvalue4 uNextYearValue,
NextYearValue uPreviousYearValue
FROM FieldTable
) as uFieldTable
ON FieldTable.id = uFieldTable.id
SET
value2 = uvalue2,
value3 = uvalue3,
value4 = uvalue4,
NextYearValue = uNextYearValue,
PreviousYearValue = uPreviousYearValue;
当然考虑到你有独特的ID。
在 Sql Server 上,事情变得更复杂,因为您可能必须使用Common Table Expressions
WITH FieldTable2 AS(
SELECT id, value2 + value3 value2, value3, value4, NextYearValue FROM FieldTable
) ,FieldTable3 AS(
SELECT id, value2, value3 + value2 value3, value4, NextYearValue FROM FieldTable2
) ,FieldTable4 AS(
SELECT id, value2, value3, value3 + value2 value4, NextYearValue FROM FieldTable3
) ,FieldTable5 AS(
SELECT id, value2, value3, value4, value2 + value3 + value4 NextYearValue FROM FieldTable4
)
UPDATE FieldTable
SET
value2 = FieldTable5.value2,
value3 = FieldTable5.value3,
value4 = FieldTable5.value4,
NextYearValue = FieldTable5.NextYearValue
FROM
FieldTable5
WHERE FieldTable.id = FieldTable5.id
答案 2 :(得分:0)
update
语句中赋值的右侧在被同一语句中的任何其他赋值修改之前进行求值。只需对两个表达式应用相同的计算,你应该没问题:
UPDATE @Tbl
SET value1 = value1 + value1,
value2 = value1 + value1
答案 3 :(得分:0)
{% set album = ['one','two'] %}
{% set one = ['a','b','c'] %}
{% set two = ['d','e','f'] %}
{% for alb in album %}
{% for songs in alb %}
{{ songs }}
{%- endfor %}
{%- endfor %}