Ordered Value by Column / Row

后端 未结 4 981
借酒劲吻你
借酒劲吻你 2021-01-16 15:24

Its a little difficult to explain. It might be easier to skip to the examples.

A table has an id and four columns that each allow null.

ID, Col1, Co         


        
4条回答
  •  情书的邮戳
    2021-01-16 15:41

    Use:

    SELECT DISTINCT COL1 AS col
      FROM YOUR_TABLE
     WHERE col1 IS NOT NULL
    UNION
    SELECT DISTINCT COL2 AS col
      FROM YOUR_TABLE
     WHERE col2 IS NOT NULL
    UNION 
    SELECT DISTINCT COL3 AS col
      FROM YOUR_TABLE
     WHERE col3 IS NOT NULL
    UNION
    SELECT DISTINCT COL4 AS col
      FROM YOUR_TABLE
     WHERE col4 IS NOT NULL
    ORDER BY col
    

    UNION will remove duplicates between the statements; DISTINCT will return a unique list of values per statement. UNION ALL would be faster than UNION, but it doesn't remove duplicates.

提交回复
热议问题