Select distinct values from multiple columns in same table

前端 未结 4 1605
[愿得一人]
[愿得一人] 2021-01-01 10:54

I am trying to construct a single SQL statement that returns unique, non-null values from multiple columns all located in the same table.

 SELECT distinct tb         


        
4条回答
  •  别那么骄傲
    2021-01-01 11:28

    try something like SubQuery:

    SELECT derivedtable.NewColumn
    FROM
    (
        SELECT code_1 as NewColumn FROM tbl_data 
        UNION
        SELECT code_2 as NewColumn FROM tbl_data 
    ) derivedtable
    WHERE derivedtable.NewColumn IS NOT NULL
    

    The UNION already returns DISTINCT values from the combined query.

提交回复
热议问题