Compare values of two columns then select the larger value

前端 未结 6 714
孤街浪徒
孤街浪徒 2021-02-12 11:30

I need to query a table and select 3 of the values of 4 columns. I need to compare the values of the 3rd column and the fourth column and select the larger value.

For ex

6条回答
  •  借酒劲吻你
    2021-02-12 11:46

    I would start by creating a view.

    CREATE VIEW t_c
    SELECT id, c1 AS c FROM t
    UNION
    SELECT id, c2 AS c FROM t;
    

    Then I would select from that view.

    SELECT id, MAX(c) FROM t_c GROUP BY id;
    

提交回复
热议问题