SQL“输入”语句匹配任何内容

发布于 2024-08-13 18:34:08 字数 396 浏览 5 评论 0原文

如果我有这样的查询,

SELECT * FROM table1 WHERE col1 IN ({SUBS})

有什么可以替换 {SUBS} 来返回表中的所有行吗?

更多详细信息:

我正在我的应用程序中动态构建 SQL,因此我不能(不应该)编辑查询的其他部分(除了大括号中的内容)。所以,

SELECT * FROM table1

不会做。

而且,

SELECT * FROM table1 WHERE col1 IN (SELECT col1 FROM table1)

这将是黑客行为并且效率极低。假设该表有超过 50k 行。

If I have a query like this

SELECT * FROM table1 WHERE col1 IN ({SUBS})

Is there anything I can replace {SUBS} with that will return all rows in the table?

Further details:

I am building the SQL dynamically in my app, so I cannot (should not) edit other parts of the query except what's in braces. So,

SELECT * FROM table1

will not do.

Also,

SELECT * FROM table1 WHERE col1 IN (SELECT col1 FROM table1)

would be hackish and highly inefficient. Consider the table have more than 50k rows.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

睡美人的小仙女 2024-08-20 18:34:08

这会做到这一点:

select col1 from table1

编辑: 似乎有点混乱 - OP询问可以使用什么值来替换 {SUBS} ,这将返回来自 < 的所有行代码>表1。我上面的答案是您可以使用代替 {SUBS} 来返回所有行。

This would do it:

select col1 from table1

Edit: There seems to be a bit of confusion - the OP asked what value could be used to replace {SUBS} that would return all rows from table1. My answer above is what you could use in place of {SUBS} that would return all the rows.

粉红×色少女 2024-08-20 18:34:08

这在 SQL Server 中适用于我:

SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME IN (COLUMN_NAME)

您是否尝试过仅使用 COL1 来表示 {SUBS}

例如

SELECT * FROM table1 WHERE col1 IN (col1)

This works for me in SQL Server:

SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME IN (COLUMN_NAME)

Have you tried just using COL1 for {SUBS}?

e.g.

SELECT * FROM table1 WHERE col1 IN (col1)
£噩梦荏苒 2024-08-20 18:34:08

如果将 {SUBS} 替换为 SELECT col1 FROM table1,则最终会

SELECT * FROM table1 WHERE col1 IN (SELECT col1 FROM table1);

返回 table1 中的所有行。当然,这只是一种更迂回的说法:

SELECT * FROM table1;

If you replaced {SUBS} with SELECT col1 FROM table1, you would end up with

SELECT * FROM table1 WHERE col1 IN (SELECT col1 FROM table1);

which would return all rows from table1. This is, of course, simply a more roundabout way of saying:

SELECT * FROM table1;
萌无敌 2024-08-20 18:34:08

你是对的,

SELECT * FROM table1 WHERE col1 IN (SELECT col1 FROM table1)

确实有效,但效率很低;需要合并连接来返回所有行。

常规 SELECT * FROM table1 一样高效

SELECT * FROM table1 WHERE col1 IN (col1)

使用以下内容与 我建议您与尝试强加 SELECT * FROM table1 WHERE col1 IN ({SUBS}) 结构的人聊聊。没有充分理由这样做。

  • 它不必要地使查询复杂化。
  • 造成查询效率极低的风险。
  • 甚至可能限制开发人员使用某些技术。

我怀疑实施此操作的人正在尝试实现某种银弹框架。请记住,软件开发中的黄金法则没有银弹

You're right,

SELECT * FROM table1 WHERE col1 IN (SELECT col1 FROM table1)

does work, but is highly inefficient; requiring a merge join to return all rows.

Use the following which is just as efficient as regular SELECT * FROM table1

SELECT * FROM table1 WHERE col1 IN (col1)

However, that said; I suggest you have a chat to the person who is trying to impose the SELECT * FROM table1 WHERE col1 IN ({SUBS}) structure. There is no good reason to do so.

  • It unnecessarily complicates queries.
  • Creates risk of highly inefficient queries.
  • Potentially even limits developers to use certain techniques.

I suspect the person imposing this is trying to implement some sort of silver-bullet framework. Remember, the golden rule in software development is that there are no silver-bullets.

淡淡離愁欲言轉身 2024-08-20 18:34:08

如果您只是想检索表中的每一行,那么:

select * from table1

如果您想证明一点或赢得赌注或其他什么,那么:

select * from table1 where col1 in (select col1 from table1)

If you're simply trying to retrieve every row in the table, then:

select * from table1

If you're trying to prove a point or win a bet or something, then:

select * from table1 where col1 in (select col1 from table1)
幸福丶如此 2024-08-20 18:34:08

如果查询需要某些 WHERE 条件,那么我会尝试将其替换为 EXISTS 语句:

select
  *
from
  table1 t1
where
  exists ( {subs} )

然后可以将 {subs} 替换为任何不会产生 NULL 的表达式。

If the query requires some WHERE condition, then I would try to replace it with an EXISTS statement:

select
  *
from
  table1 t1
where
  exists ( {subs} )

Then {subs} can be replaced with any expression that does not yield NULL.

心碎的声音 2024-08-20 18:34:08

这在 Oracle 中有效:

select * from table1 where col1 in (col1)

This works in Oracle:

select * from table1 where col1 in (col1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文