简体   繁体   English

SQL“In”语句匹配任何内容

[英]SQL “In” Statement Match Anything

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? 有什么我可以替换{SUBS},它将返回表中的所有行?

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. 我正在我的应用程序中动态构建SQL,所以我不能(不应该)编辑查询的其他部分,除了括号中的内容。 So, 所以,

SELECT * FROM table1

will not do. 不行。

Also, 也,

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

would be hackish and highly inefficient. 将是hackish和非常低效。 Consider the table have more than 50k rows. 考虑表有超过50k行。

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 . 编辑:似乎有点混乱 - OP询问可以使用什么值来替换将返回table1所有行的{SUBS} My answer above is what you could use in place of {SUBS} that would return all the rows. 我上面的答案就是你可以使用什么代替返回所有行的{SUBS}

This works for me in SQL Server: 这适用于SQL Server:

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

Have you tried just using COL1 for {SUBS} ? 您是否尝试过将COL1用于{SUBS}

eg 例如

SELECT * FROM table1 WHERE col1 IN (col1)

If you replaced {SUBS} with SELECT col1 FROM table1 , you would end up with 如果用SELECT col1 FROM table1替换{SUBS} ,你最终会得到

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

which would return all rows from table1 . 这将返回table1所有行。 This is, of course, simply a more roundabout way of saying: 当然,这只是一种更为迂回的说法:

SELECT * FROM table1;

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一样高效

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. 我建议您与试图强制使用SELECT * FROM table1 WHERE col1 IN ({SUBS})结构的人聊天。 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 . 请记住,软件开发的黄金法则没有 银子弹

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)

If the query requires some WHERE condition, then I would try to replace it with an EXISTS statement: 如果查询需要一些WHERE条件,那么我会尝试用EXISTS语句替换它:

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

Then {subs} can be replaced with any expression that does not yield NULL. 然后{subs}可以替换为不产生NULL的任何表达式。

这适用于Oracle:

select * from table1 where col1 in (col1)

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

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