简体   繁体   English

如何在PHP中的Mysql查询结果中获取最小值列的字段名称

[英]How to get the Field Name of the Minimum Value Column in Mysql query Result in PHP

I have a Result Set as below. 我有一个结果集如下。 I want to get the name of the Lowest/Least/Minimum Value . 我想获得最低/最低/最低值的名称。 In the Picture Below ie the Lowest is "14" and the Name of the Field is Sum(c.2). 在下面的图片中,即最低的是“ 14”,字段的名称是Sum(c.2)。 So how could I get the field name like "Sum(c.2) " only which has the Minimum Value/Maximum Value? 那么,如何才能获得仅具有最小值/最大值的字段名称,例如"Sum(c.2) ”?

MySQL的结果集

SELECT
     dater,
     LEAST(c0_sum,c1_sum,c2_sum,c3_sum,c4_sum,c5_sum,c6_sum,c7_sum,c8_sum,c9_sum) as minimum_val,
     GREATEST(c0_sum,c1_sum,c2_sum,c3_sum,c4_sum,c5_sum,c6_sum,c7_sum,c8_sum,c9_sum) as maximum_val
 FROM result_set

This will work fine. 这样就可以了。 Hope this helps 希望这可以帮助

I think this will work fine for you. 我认为这对您来说很好。

SELECT @LeastVar:= LEAST(`sum(c.0)`, `sum(c.1)`, `sum(c.2)`, `sum(c.3)`, `sum(c.4)`, `sum(c.5)`, `sum(c.6)`, `sum(c.7)`, `sum(c.8)`, `sum(c.9)`) AS LeastVar,
   CASE @LeastVar WHEN `sum(c.0)` THEN 'sum(c.0)'
                  WHEN `sum(c.1)` THEN 'sum(c.1)'
                  WHEN `sum(c.2)` THEN 'sum(c.2)'
                  WHEN `sum(c.3)` THEN 'sum(c.3)'
                  WHEN `sum(c.4)` THEN 'sum(c.4)'
                  WHEN `sum(c.5)` THEN 'sum(c.5)'
                  WHEN `sum(c.6)` THEN 'sum(c.6)'
                  WHEN `sum(c.7)` THEN 'sum(c.7)'
                  WHEN `sum(c.8)` THEN 'sum(c.8)'
                  WHEN `sum(c.9)` THEN 'sum(c.9)'
       END AS LeastVarColumn
   FROM result_set

With this Query you get the minimum Value and the Column Name 通过此查询,您将获得最小值列名

SELECT 
      @v:=least(c0_sum,c1_sum,
                c2_sum,c3_sum,c4_sum) AS min_value,
      if(c0_sum = @v, 'c0_sum',
      if(c1_sum = @v, 'c1_sum',
      if(c2_sum = @v, 'c2_sum',
      if(c3_sum = @v, 'c3_sum',
      if(c4_sum = @v, 'c4_sum','error'))))) as min_column
 FROM result_set;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM