SQL在同一列中选择不同的值 [英] SQL Select Diferent Values In the Same Column

查看:111
本文介绍了SQL在同一列中选择不同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我想知道是否可以从同一表的2个不同列中选择1个值并将它们显示在同一列中.

这是一个例子.

Hello,

I wanted to know if it is possible to select 1 value from 2 diferent Columns in the same table and display them in the same column.

Heres an Example.

Total Free Space     |     Total Space
10                            30
20                            10
40                            20


显示类似的内容:


Show something like:

10 --First Column
30 --Second Column


谢谢.
最好的问候.


Thank You.
Best Regards.

推荐答案

SELECT F1 
FROM Table1
UNION ALL
SELECT F2 AS F1 
FROM Table2



有关MSDN的更多信息,请访问: UNION [



More at MSDN: UNION[^]


Analogically, if data comes from the same table:

SELECT F1 
FROM Table1
UNION ALL
SELECT F2 AS F1 
FROM Table1


[/EDIT]


[/EDIT]


您可以为此使用全部联合
例如
you can use union all for this
e.g.
select column_Name1 from tbl_1 union all select column_Name2 from tbl_2


您尚未提及要使用的确切逻辑,因为答案可能会根据需要而有所不同.但是,例如,如果您想从查询中的某个列中选择一个值,则可以使用 CASE [ ^ ]

一个例子
You haven''t mentioned the exact logic you''re trying to use since the answer may vary depending on the needs. But if you want for example pick a value from some column in your query then you can use CASE[^]

An example
SELECT CASE
          WHEN column1 = 10 THEN column1
          WHEN column2 = 30 THEN column2
          ELSE column3
       END AS ResultColumnName
FROM YourTable


这篇关于SQL在同一列中选择不同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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