2014-10-18 114 views
1

我有一个Java代码中的电子邮件地址列表,我想检查这些数据库中是否存在任何这些数据库。例如:如何检查数据库中是否存在多个值?

  1. [email protected]
  2. [email protected]
  3. [email protected]

我想知道,如果上述任何电子邮件ID是表中已经存在,如果他们这样做,我想将它们拉出或分开。
我不知道如何做到这一点?
我应该在Java中尝试这个还是有一个查询MySQL会帮助我?

回答

4
select if(count(*)>0, 'Exist', 'Not exist') 
      from email_table 
      where email='[email protected]' 

如果你只是想occurence的数量,你可以做

select count(*) from email_table where email = '[email protected]' 

如果你想在同一时间检查多个值,你可以做

select count(*) from email_table where email in ('[email protected]', '[email protected]') 

如果你想查看找到哪个ID:

select email from email_table where email in ('[email protected]', '[email protected]") 
0
import java.util.ArrayList; 
import java.util.List; 

public class EmailList { 

    public static void main(String[] args) { 
     // collect output into this list 
     List<String> existing = new ArrayList<String>(); 

     // load this list as you please, such as a file 
     List<String> source = new ArrayList<String>(); 
     source.add("[email protected]"); 
     source.add("[email protected]"); 
     source.add("[email protected]"); 

     boolean noRowProcessed = true; 
     String sql = "SELECT email FROM my_table WHERE email IN ("; 
     for(String email : source) { 
      if(!noRowProcessed){ 
       sql += ", "; 
      } 
      sql += "'" + email + "'"; 
      noRowProcessed = false; 
     } 
     sql += ")"; 

     System.out.println(sql); 


     // run your query here and collect output into list 'existing' 
    } 
} 
相关问题