我想创建一个查询来从表中选择几个列,并将结果显示为单个列。 我知道我可以使用此查询执行此操作:
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 ZThe 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.
更多推荐
发布评论