2017-10-09 78 views
1

我在SQL的表:如何在sql和php中填寫最小值?

表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 

如果I總的所有字段在單個日期然後

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?

請幫助我。

在此先感謝。

+1

哪裏是你的id怎麼做第一個表與第二個,你準行? – madalinivascu

+0

我的朋友,我不知道我將如何關聯兩個表。請幫幫我。 –

+0

我澄清了我的問題並編輯了我的問題。現在plz幫助... –

回答

0

這裏的代碼來實現這一目標:

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 
+0

朋友,我想在表1中分別填寫佣金字段40和56.請向我展示代碼和表中的值。提前致謝。 –

相關問題