如何将2列或更多列合并为一列? [英] How to merge 2 or more columns into one?

查看:134
本文介绍了如何将2列或更多列合并为一列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个真正的问题,就是要以最有效的方式将2列或更多列的数据合并为1列的SQL任务.

I have a real problem with one task in SQL concerning merging data from 2 and more columns into 1 column in the most effective way.

id   column1   column2  column3 
1    ok                    notOK
2    
3    abraka     dabrra
4    miew                    haf

我需要将3条评论合并为1条评论列

and I need to merge 3 comments into 1 comment column like this

id   comments
1    ok                    
1    notOK
2    
3    abraka     
3    dabrra
4    miew                    
4 haf

现在,我通过插入具有ID和Comments列的表来手动完成此操作,并且必须从主表中整理出数据.这确实很耗时,尤其是当我有至少8个要合并的注释列时.

Now I do it manually through insert into table where I have id and comments columns and I have to sort out data from the primary table. It is really time-consuming, especially when I have at least 8 comments columns that I want to merge.

推荐答案

尝试此查询

Select Id, Comments 
From 
(
    Select Id, Column1 Comments From MyTable Where Column1 Is Not Null
    Union All
    Select Id, Column2 Comments From MyTable Where Column2 Is Not Null
    Union All
    Select Id, Column3 Comments From MyTable Where Column3 Is Not Null
) DerivedTable
Order by Id

这篇关于如何将2列或更多列合并为一列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆