有一个数组,user1,user2,user9
有一个用户表,里面有很多用户,比如user1,user2,user3,user4等等。
我现在要查询这个数组中的所有用户如果在用户表中只要有一个不存在那么返回0,如果数组中所有的用户在用户表中都存在那么返回1,这条SQL语句怎么写呢?
有一个用户表,里面有很多用户,比如user1,user2,user3,user4等等。
我现在要查询这个数组中的所有用户如果在用户表中只要有一个不存在那么返回0,如果数组中所有的用户在用户表中都存在那么返回1,这条SQL语句怎么写呢?
我给你一个解决方案,你看看能不能满足一下你的需求比如你的书组 string []a = {"123","1234","12345"};
string str = "";
for(i=0; i<a.length; i++)
{
str += "'" + a[i] + "',";
}
str = str.substring(0, str.length-1);
strsql = "select * from table1 where id in (" + str + ")";
strsql = "select count(*) from (" + strsql + ")"然后执行这个语句,如果得到的值 等于a数组的长度,那么就表示都存在,如果小于数组a的长度,那么就,当然,条件是数组中元素不重复,表中id也是unique的
不足的是,当数组很大时,效率会下降很多
select decode(col, 3, 1, 0) e
from (select nvl((select 1 from t_user where username = 'user1'), 0) +
nvl((select 1 from t_user where username = 'user2'), 0) +
nvl((select 1 from t_user where username = 'user9'), 0) col
from dual)
都存在才返回1,你写的是存在记录的条数,明显跟题意不符。
manyroads的写得很不错,不过正如你所说的那样,如果数据量很大的话,效率确实会下降。
那个用户的表假设叫user_table
select count(*)
from (
select user_name
from temp_table
minus
select user_name
from user_table
)
如果count(*)的数目=0,表示数组里的数据在user_table中都存在,如果>0,表示数组里的表在user_table中不是都存在