2014-10-18 141 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' 
    } 
} 
相關問題