如何从表格

时间:2018-06-15 13:18:41

标签: mysql sql database mysqli

问题1

对于ID_CAR,如何从表(非空)中获取最低值?例如,对于ID_CAR 1,最低值为50,对于ID_CAR 2,最低值为50;对于ID_CAR 3,最低值为300.我不需要重复,我只需要一个汽车的一个值。

ID_CAR | col_1 | col_2 | col_3 | col_4 | col_5 | col_6 

1      | null  | 250   | 300   | null  | 900   | null
2      | 100   | null  | 300   | 600   | 200   | 100
1      | 300   | 100   | 800   | 100   | 50    | 900
3      | 300   | 4000  | null  | null  | null  | null
2      | null  | null  | null  | 50    | null  | 100
4      | 400   | 900   | 500   | 700   | 800   | 500

问题2 在此示例中,col_ *中的值是天。我需要为col_date添加天数并获得最低值。例如,ID_CAR 1的最低日期是2018-01-03(col_2),ID_CAR 2的最低日期是2018-01-15(col_4)。

ID_CAR | col_1 | col_2 | col_3 | col_4 | col_5 | col_6 | col_date

1      | null  | 2     | 3     | null  | 5     | null  | 2018-01-01
2      | 1     | null  | 3     | 6     | 10    | 10    | 2018-01-13
1      | 3     | 20    | 80    | 10    | 50    | 90    | 2018-01-02
3      | 30    | 40    | null  | null  | null  | null  | 2018-01-03
2      | null  | null  | null  | 5     | null  | 10    | 2018-01-10
4      | 10    | 9     | 5     | 70    | 8     | 50    | 2018-01-07

5 个答案:

答案 0 :(得分:5)

如果没有union,您只需合并leastmin功能:

select
  ID_CAR,min(least(col_1,col_2,col_3,col_4,col_5,col_6)) lowest_value
from
  table
group by
  ID_CAR

如果您有null个值,则需要ifnullcoalesce函数

select
  ID_CAR,
  min(least(
    ifnull(col_1,~0),
    ifnull(col_2,~0),
    ifnull(col_3,~0),
    ifnull(col_4,~0),
    ifnull(col_5,~0),
    ifnull(col_6,~0)
  )) as lowest_value
from
  table
group by
  ID_CAR
  • ~0是mysql中的最大bigint
  • least的相反功能是greatest
  • min的相反功能是max; - )

适用于Mysql,Oracle,Postgres,Hive ......

问题2 ,类似这样:

select
  ID_CAR,
  min(least(
    DATE_ADD(col_date, INTERVAL ifnull(col_1,0) DAY),
    DATE_ADD(col_date, INTERVAL ifnull(col_2,0) DAY),
    DATE_ADD(col_date, INTERVAL ifnull(col_3,0) DAY),
    DATE_ADD(col_date, INTERVAL ifnull(col_4,0) DAY),
    DATE_ADD(col_date, INTERVAL ifnull(col_5,0) DAY),
    DATE_ADD(col_date, INTERVAL ifnull(col_6,0) DAY)
  )) as lowest_date
from
  table
group by
  ID_CAR

或者像这样(除非所有列都可以为null):

select
  ID_CAR,
  DATE_ADD(col_date, INTERVAL min(least(
    ifnull(col_1,~0),
    ifnull(col_2,~0),
    ifnull(col_3,~0),
    ifnull(col_4,~0),
    ifnull(col_5,~0),
    ifnull(col_6,~0)
  )) DAY) as lowest_date
from
  table
group by
  ID_CAR

答案 1 :(得分:2)

以下查询将为您提供所需的结果

select tab.ID_CAR, min(tab.val) as lowest_value from
(
      (select ID_CAR,min(col_1) val
      from table
      group by ID_CAR)
  union
      (select ID_CAR,min(col_2) val
      from table
      group by ID_CAR)
  union
      (select ID_CAR,min(col_3) val
      from table
      group by ID_CAR)
  union
      (select ID_CAR,min(col_4) val
      from table
      group by ID_CAR)
  union
      (select ID_CAR,min(col_5) val
      from table
      group by ID_CAR)
  union
      (select ID_CAR,min(col_6) val
      from table
      group by ID_CAR)
) tab
group by tab.ID_CAR

答案 2 :(得分:2)

您需要UNION

select id_car, min(val) as lowest_value
from (select id_car, col_1 as Val
      from table union 
      select id_car, col_2
      from table
      . . .
     ) t 
group by id_car;

答案 3 :(得分:2)

试试这个 如果您期望值大于9999999999999999999,则使用更高的值

select id_car,
min(least(coalesce(col_1,9999999999999999999),coalesce(col_2,9999999999999999999),coalesce(col_3,9999999999999999999),
          coalesce(col_4,9999999999999999999),coalesce(col_5,9999999999999999999),coalesce(col_6,9999999999999999999)
        )
    ) as min_val 
from your_table
group by id_car

答案 4 :(得分:1)

天真的方法将至少使用:

SELECT ID_CAR, least(t.col_1, t.col_2, t.col_3, t.col_4, t.col_5, t.col_6)
FROM
(SELECT ID_CAR, min(col_1) as col_1, min(col_2) as col_2, min(col_3) as col_3, min(col_4) as col_4, min(col_5) as col_5, min(col_6) as col_6
FROM YOUR_TABLE GROUP BY ID_CAR) t;

但是:如果LEAST的任何参数为NULL,它将返回NULL。您需要将NULL转换为高值(这是一个黑客但会在实践中工作,请参阅其他答案)。

这意味着做这样的事情:

SELECT ID_CAR, LEAST(col_1, col_2, col_3,
                     col_4, col_5, col_6) as l
FROM
(SELECT ID_CAR, 
   IFNULL(min(col_1), 9999)  as col_1, 
   IFNULL(min(col_2), 9999)  as col_2,
   IFNULL(min(col_3), 9999)  as col_3,
   IFNULL(min(col_4), 9999)  as col_4,
   IFNULL(min(col_5), 9999)  as col_5,
   IFNULL(min(col_6), 9999)  as col_6
FROM YOUR_TABLE GROUP BY ID_CAR) t;

但是,使用技巧转换表可能会很好 进入表格的三行表:

car_id | attr | value
     1      1    NULL   ; or use strings such as "size"
     1      2     250