2014-02-25 163 views
0

嗨我写的mysql/oracle数据库查询,支持选择所有的条款我的查询是这样的MySQL的条件里面IN子句

SELECT * FROM country 
WHERE 
country_id in(IF('test' = 'test',(1,2,3),true)) 

如果条件(“测试” =“测试”)为真那么它应该火查询像

SELECT * FROM country WHERE country_id in(1,2,3) 

否则它应该火查询

SELECT * FROM country WHERE country_id in(true) 
+0

好吧那有什么意义? – Deepak

+1

任何问题或疑问?你只是想让我们知道吗? – Fabio

+0

有什么感觉:'in(true)'? –

回答

4

您可以使用test可变这里,如果test1 = test2然后country_id将在(1,2,3)否则就没有限制了country_id

SELECT * FROM country 
WHERE ((test1 = test2 and country_id in(1,2,3))or 
     test1 <> test2) 
+0

嘿,它不在ORACLE工作 –

+0

它在'ORACLE'很好。看看http://sqlfiddle.com/#!4/6d28a/2 – Hamidreza

3

你可以用基本的布尔做到这一点逻辑。你在做什么就相当于:

SELECT * 
FROM country 
WHERE country_id in (1,2,3) or 'test' <> 'test'; 

(注:我不'test'测试为NULL,因为它是一个文本值如果这是真的,可以采取一个NULL值的变量,你必须要考虑到这一点为好)