SQL“输入”语句匹配任何内容
如果我有这样的查询,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这会做到这一点:
编辑: 似乎有点混乱 - OP询问可以使用什么值来替换
{SUBS}
,这将返回来自 < 的所有行代码>表1。我上面的答案是您可以使用代替{SUBS}
来返回所有行。This would do it:
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 fromtable1
. My answer above is what you could use in place of{SUBS}
that would return all the rows.这在 SQL Server 中适用于我:
您是否尝试过仅使用 COL1 来表示 {SUBS}?
例如
This works for me in SQL Server:
Have you tried just using COL1 for {SUBS}?
e.g.
如果将
{SUBS}
替换为SELECT col1 FROM table1
,则最终会返回
table1
中的所有行。当然,这只是一种更迂回的说法:If you replaced
{SUBS}
withSELECT col1 FROM table1
, you would end up withwhich would return all rows from
table1
. This is, of course, simply a more roundabout way of saying:你是对的,
确实有效,但效率很低;需要合并连接来返回所有行。
常规 SELECT * FROM table1 一样高效
使用以下内容与 我建议您与尝试强加 SELECT * FROM table1 WHERE col1 IN ({SUBS}) 结构的人聊聊。没有充分理由这样做。
我怀疑实施此操作的人正在尝试实现某种银弹框架。请记住,软件开发中的黄金法则是没有银弹。
You're right,
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
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.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.
如果您只是想检索表中的每一行,那么:
如果您想证明一点或赢得赌注或其他什么,那么:
If you're simply trying to retrieve every row in the table, then:
If you're trying to prove a point or win a bet or something, then:
如果查询需要某些 WHERE 条件,那么我会尝试将其替换为 EXISTS 语句:
然后可以将 {subs} 替换为任何不会产生 NULL 的表达式。
If the query requires some WHERE condition, then I would try to replace it with an EXISTS statement:
Then {subs} can be replaced with any expression that does not yield NULL.
这在 Oracle 中有效:
This works in Oracle: