如何在sql和php中填写最小值?
How to fill min value in sql and php?
我在 SQL 中有一个 table:
Table 1
date name s1 s2 s3 s4 min_value
7/10/2017 ram 10 11 12 13 11
7/10/2017 tom 17 16 15 14 16
7/10/2017 ara 13 9 26 22 9
如果我在一个日期内计算所有字段的总和,那么
s1 total value 40
s2 total value 36
s3 total value 53
s4 total value 49
这里minimum value
是36,意思是minimum value
列是s2
.
我分别在min_value
字段中插入了s2字段值
我的问题是how will I fill min_value field?
请帮帮我。
提前致谢。
实现代码如下:
SELECT [date], s1, s2, s3, s4,
case when s1 < s2 and s1 < s3 and s1 < s4 then s1
when s2 < s1 and s2 < s3 and s2 < s4 then s2
when s3 < s1 and s3 < s2 and s3 < s4 then s3
else s4 end as [min_value]
FROM (
SELECT [date], sum(s1) AS s1, sum(s2) AS s2, sum(s3) AS s3, sum(s4) AS s4 FROM [Table1]
GROUP BY [date]
) AS A
我在 SQL 中有一个 table:
Table 1
date name s1 s2 s3 s4 min_value
7/10/2017 ram 10 11 12 13 11
7/10/2017 tom 17 16 15 14 16
7/10/2017 ara 13 9 26 22 9
如果我在一个日期内计算所有字段的总和,那么
s1 total value 40
s2 total value 36
s3 total value 53
s4 total value 49
这里minimum value
是36,意思是minimum value
列是s2
.
我分别在min_value
字段中插入了s2字段值
我的问题是how will I fill min_value field?
请帮帮我。
提前致谢。
实现代码如下:
SELECT [date], s1, s2, s3, s4,
case when s1 < s2 and s1 < s3 and s1 < s4 then s1
when s2 < s1 and s2 < s3 and s2 < s4 then s2
when s3 < s1 and s3 < s2 and s3 < s4 then s3
else s4 end as [min_value]
FROM (
SELECT [date], sum(s1) AS s1, sum(s2) AS s2, sum(s3) AS s3, sum(s4) AS s4 FROM [Table1]
GROUP BY [date]
) AS A