简体   繁体   English

创建多行“表”作为SELECT的一部分

[英]Creating a multi-row “table” as part of a SELECT

I'm not really sure how to describe my question (thus the awful title), but it's related to this recent question . 我不太确定如何描述我的问题(因此标题很糟糕),但这与最近的问题有关

The problem would be easily solved if there was some way for me to create a "table" with 4 rows as part of my SELECT (to use with NOT IN or MINUS). 如果我可以通过某种方式创建一个包含4行的“表”作为SELECT的一部分(与NOT IN或MINUS一起使用),则可以轻松解决该问题。

What I mean is, I can do this: 我的意思是,我可以这样做:

SELECT 1, 2, 3, 4;

And will receive one row from the database: 并且将从数据库中接收一行:

| 1 | 2 | 3 | 4 |

But is there any way to receive the following (without using UNION, I don't really want a query that's potentially thousands of lines long with a long list)? 但是有什么方法可以接收以下内容(不使用UNION,我真的不想要一个可能长成千上万行且带有长列表的查询)吗?

| 1 |
| 2 |
| 3 |
| 4 |

This kind of query is possible in databases that support recursive queries (postgres, oracle, db2, mssql to name a few). 在支持递归查询的数据库(postgres,oracle,db2,mssql等)中,这种查询是可能的。

In mysql your (conceptually) easiest path would be to do create temporary table and then insert the IDs you are interested in as suggested here 在mysql中,(从概念上)最简单的路径是创建临时表,然后按照此处的建议插入您感兴趣的ID

If you want it nicer, you could put the create temp table in mysql stored procedure . 如果希望更好,可以将create temp表放在mysql 存储过程中

The best way I've found to split a delimited constant input into a table is using a numbers / tally table - see http://www.sqlteam.com/article/parsing-csv-values-into-multiple-rows for more info. 我发现将定界常量输入拆分为表格的最佳方法是使用数字/计数表-有关更多信息,请参见http://www.sqlteam.com/article/parsing-csv-values-into-multiple-rows信息。 I've used this technique before on large datasets and found it pretty fast. 我以前在大型数据集上使用过这种技术,并且发现它非常快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM