从同一个表中选择多个列而不使用表联合(Select multiple columns from the same table without tables union)

编程入门 行业动态 更新时间:2024-10-20 00:39:25
从同一个表中选择多个列而不使用表联合(Select multiple columns from the same table without tables union)

我想创建一个查询来从表中选择几个列,并将结果显示为单个列。 我知道我可以使用此查询执行此操作:

select a from Z union select b from Z union select c from Z union select d from Z ....

但在我的情况下,表Z是一个约50行的子查询,我不想复制和粘贴。 所以我想在Z只出现一次的查询中有这个。 我不知道这是否可行。

你知道这样做的方法吗?

先谢谢你。

问候。

I would like to create a query to select several columns from a table, and show the results as a single column. I know I can do this with this query:

select a from Z union select b from Z union select c from Z union select d from Z ....

But in my case, table Z is a sub-query of about 50 lines, that I would not want to copy and paste. So I would like to have this in a query where Z appears only once. I don't know if this is possible.

Do you know a way to do that?

Thank you in advance.

Regards.

最满意答案

您可以使用WITH子句来完成它:

with Z as ( select ... from ... -- <<== Put your big query here ) select a from Z union select b from Z union select c from Z union select d from Z

顶部的with子句使Z可用于查询的其余部分,而不必重复自己。

这是sqlfiddle上的一个演示 。

You can do it using a WITH clause:

with Z as ( select ... from ... -- <<== Put your big query here ) select a from Z union select b from Z union select c from Z union select d from Z

The with clause at the top makes Z available to the remaining parts of your query without having to repeat yourself.

Here is a demo on sqlfiddle.

更多推荐

本文发布于:2023-07-06 08:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047616.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   而不   Select   multiple   union

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!