mysql条件inside-IN子句

mysql条件inside-IN子句,mysql,sql,oracle,Mysql,Sql,Oracle,您好,我正在编写mysql/oracle数据库查询,以支持SELECTALL for子句,我的查询是这样的 SELECT * FROM country WHERE country_id in( IF('test' = 'test',(1,2,3),true) ) 如果条件'test'='test'为true,那么它应该触发如下查询 SELECT * FROM country WHERE country_id in(1,2,3) 否则它应该引发查询 SELECT * FROM count

您好,我正在编写mysql/oracle数据库查询,以支持SELECTALL for子句,我的查询是这样的

SELECT * FROM country
WHERE 
country_id in( IF('test' = 'test',(1,2,3),true) )
如果条件'test'='test'为true,那么它应该触发如下查询

SELECT * FROM country WHERE  country_id in(1,2,3)
否则它应该引发查询

 SELECT * FROM country WHERE  country_id in(true)

您可以使用基本布尔逻辑来实现这一点。您所做的工作相当于:

SELECT *
FROM country
WHERE country_id in (1,2,3) or 'test' <> 'test';
注意:我没有在“test”上测试NULL,因为它是一个文本值。如果这真的是一个可以接受空值的变量,您也必须考虑到这一点。

您可以在这里使用test作为变量,如果test1=test2,则国家id将在1,2,3中,否则国家id将不受限制:


好的,那重点是什么?有问题吗?你只是想让我们知道吗?这是什么意思:真的?
SELECT * FROM country
WHERE ((test1 = test2 and country_id in(1,2,3))or
        test1 <> test2)