简体   繁体   English

SQL:将IN运算符与任何内容匹配

[英]SQL : Match IN operator with anything

I am trying to build a dynamic query. 我正在尝试建立一个动态查询。

This works fine : 这工作正常:

$qstring="10000, 10001, 10003, 10005";

SELECT *
FROM orders
WHERE order_id in ($qstring);

This also works fine when I have no data to match and select. 当我没有要匹配和选择的数据时,这也可以正常工作。

$qstring="";

SELECT *
FROM orders
WHERE order_id in ($qstring);

Does something like below exists to match any data? 是否存在以下类似内容以匹配任何数据? I checked out the pattern matching for SQL but found nothing that could match everything. 我检查了SQL的模式匹配,但没有发现可以匹配所有内容的内容。

$qstring="*";

SELECT *
FROM orders
WHERE order_id in ($qstring);

Any hints? 有什么提示吗? Thanks! 谢谢!

So, you need to make your query: 因此,您需要进行查询:

SELECT *
FROM orders
WHERE order_id in ($qstring);

Works fine in case there was no values passed in the @qstring parameter. 如果在@qstring参数中没有传递任何值,则工作@qstring In this case try this instead: 在这种情况下,请尝试以下操作:

SELECT * FROM orders 
WHERE ($qstring <> '*' AND order_id in ($qstring));

If you want to match everything just use the select without the where clause. 如果要匹配所有内容,请使用不带where子句的select。

SELECT * FROM orders

If you're forced (for some odd reason to use the construct you described) then... 如果您由于某种奇怪的原因而被迫使用您描述的结构,那么...

$qstring="SELECT order_id FROM orders";

In case you need everything you should not have a WHERE IN clause. 如果您需要所有内容,则不应有WHERE IN子句。

Check your $qstring variable and only append the WHERE order_id in ($qstring); 检查您的$qstring变量,仅WHERE order_id in ($qstring);附加WHERE order_id in ($qstring); when neccessary 必要时

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

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