How SQL merges two columns of data into one column of data


Preface

In this article, we will explain how to merge two columns of data into one column in SQL. In many cases, we need to merge data from two columns into one column for better analysis and processing of data.


First, use the CONCAT function to merge columns

The following is an example of using the CONCAT function to combine the "first_name" column and the "last_name" column into one "full_name" column:

SELECT CONCAT(first_name, '/', last_name) AS full_name
FROM employees;

In the above example, we concatenate the "first_name" column and the "last_name" column using the space character and name the merged result as the "full_name" column.

Use the addition operation to match the parallel

In addition to using the CONCAT function, we can also use the addition operator ("+") to combine the values ​​of two columns into one column. This is useful when you need to merge two columns of integer type.

The following is an example of using the addition operator to combine the "birth_year" column and the "age" column into one "year_and_age" column:

SELECT birth_year + age AS year_and_age
FROM users;

In the above example, we add the values ​​of the "birth_year" column and the "age" column and name the result as the "year_and_age" column.

It should be noted that when using the addition operation to comply with parallelism, ensure that the data types of the two columns are compatible. For example, you can merge a column of type integer with a column of type string, but you cannot merge a column of type string with a column of type date.

Merge columns using CASE statement

In some cases, we need to merge data from two columns into one column based on conditions. At this time, you can use the CASE statement to achieve.
The following is an example of using the CASE statement to combine the "gender" column and the "age" column into a single "category" column:

SELECT 
    CASE 
        WHEN gender = 'Male' AND age < 18 THEN 'Boy'
        WHEN gender = 'Male' AND age >= 18 THEN 'Man'
        WHEN gender = 'Female' AND age < 18 THEN 'Girl'
        WHEN gender = 'Female' AND age >= 18 THEN 'Woman'
        ELSE 'Unknown'
    END AS category
FROM users;

In the above example, we merged the "gender" column and the "age" column into one "category" column based on different conditions. We assign different values ​​to the "category" column based on the combination of "gender" and "age".

Merge columns using UNION

If we have two columns with the same data type and want to merge them into a single result set, we can use the UNION operator.
The following is an example of using the UNION operator to combine the data of column "column_a" and column "column_b" into one result set:

SELECT column_a FROM table_a
UNION
SELECT column_b FROM table_b;

In the above example, we use the UNION operator to combine the "column_a" column of the "table_a" table and the "column_b" column of the "table_b" table into one result set.

It should be noted that when using the UNION operation to comply with parallelism, the number of columns and data types of the two tables must be the same.


Summarize

In this article, we introduce five methods of merging two columns of data into one column: using the CONCAT function, using the addition operator, using the CASE statement, and using the UNION operator. Based on actual needs, choosing the appropriate method can better process and analyze data. When using these methods, you need to pay attention to the data type and compatibility of the columns to ensure the accuracy and reliability of the merge operation.

Guess you like

Origin blog.csdn.net/hsuehgw/article/details/132611953