简体   繁体   中英

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). So how could I get the field name like "Sum(c.2) " only which has the Minimum Value/Maximum Value?

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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