获取比较两个表的其他表中不存在的所有用户名

时间:2017-03-23 07:29:00

标签: mysql

我正在尝试比较两个数据库并获取其他数据库中不存在的所有用户。对于例如数据库wp_members有正确的成员列表和数据库成员缺少一些成员然后我想比较wp_members和成员用户名,并找出成员中缺少的用户名,以便我可以添加它们。 (应选择wp_members用户名)

这是db

的SQL小提琴
http://sqlfiddle.com/#!9/6a795e

这就是我想要做的事情

SELECT m.username FROM wp_members m, member n WHERE m.username<>n.username and n.siteid=1 group by m.username

但没有成功。

2 个答案:

答案 0 :(得分:0)

可以通过public class MainActivity extends AppCompatActivity { public static Bundle myBundl = new Bundle(); private List<ItemSlideMenu> listSliding; private SlidingMenuAdapter3 adapter; private Calendar calendar; private TextView dateView; TimePicker timePicker2; final Context context = this ; private int year, month, day; private ListView listViewSliding; private DrawerLayout drawerLayout; private ActionBarDrawerToggle actionBarDrawerToggle; String name1; @RequiresApi(api = Build.VERSION_CODES.N) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); calendar = Calendar.getInstance(); year = calendar.get(Calendar.YEAR); month = calendar.get(Calendar.MONTH); day = calendar.get(Calendar.DAY_OF_MONTH); showDate(year, month+1, day); //Init component listViewSliding = (ListView) findViewById(R.id.lv_sliding_menu); drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); listSliding = new ArrayList<>(); //Add item for sliding list listSliding.add(new ItemSlideMenu(R.drawable.home_96,"Accueil" )); listSliding.add(new ItemSlideMenu(R.drawable.cocktail,"Organiser une Sortie")); listSliding.add(new ItemSlideMenu(R.drawable.geo,"Autour de moi")); listSliding.add(new ItemSlideMenu(R.drawable.ami,"Liste d'amis" )); listSliding.add(new ItemSlideMenu(R.drawable.message,"Contact")); listSliding.add(new ItemSlideMenu(R.drawable.information,"Credits")); adapter = new SlidingMenuAdapter3(this, listSliding); listViewSliding.setAdapter(adapter); //Display icon to open/ close sliding list getSupportActionBar().setDisplayHomeAsUpEnabled(true); //Set title setTitle(listSliding.get(0).getTitle()); //item selected listViewSliding.setItemChecked(0, true); //Close menu drawerLayout.closeDrawer(listViewSliding); //Display fragment 1 when start replaceFragment(0); //Hanlde on item click listViewSliding.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //Set title setTitle(listSliding.get(position).getTitle()); //item selected listViewSliding.setItemChecked(position, true); if (position==2){ Intent i2 = new Intent(getApplicationContext(), MapsActivity.class); startActivity(i2); } else {//Replace fragment replaceFragment(position); }//Close menu drawerLayout.closeDrawer(listViewSliding); } }); actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_opened, R.string.drawer_closed){ @Override public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); invalidateOptionsMenu(); } @Override public void onDrawerClosed(View drawerView) { super.onDrawerClosed(drawerView); invalidateOptionsMenu(); } }; drawerLayout.setDrawerListener(actionBarDrawerToggle); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main_menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { if(actionBarDrawerToggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); actionBarDrawerToggle.syncState(); } //Create method replace fragment private void replaceFragment(int pos) { android.support.v4.app.Fragment fragment = null; switch (pos) { case 0: fragment = new Fragment1(); break; case 1: fragment = new Fragment2(); break; case 2: fragment = new Fragment3(); break; case 3 : fragment = new Fragment4(); break; } if(null!=fragment) { android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager(); android.support.v4.app.FragmentTransaction transaction = fragmentManager.beginTransaction(); transaction.replace(R.id.main_content,fragment); transaction.addToBackStack(null); transaction.commit(); } } @SuppressWarnings("deprecation") protected Dialog onCreateDialog(int id) { // TODO Auto-generated method stub if (id == 999) { return new DatePickerDialog(this, myDateListener, year, month, day); } else if (id ==98) { final Dialog dialog = new Dialog(context); dialog.setContentView(R.layout.custom); dialog.setTitle("Title..."); // set the custom dialog components - text, image and button Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK); dialogButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Fragment6 fragment = null; fragment = new Fragment6(); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction transaction = fragmentManager.beginTransaction(); transaction.replace(R.id.contact, fragment); transaction.addToBackStack(null); transaction.commit(); dialog.dismiss(); } }); dialog.show(); // if button is clicked, close the custom dialog } return null; } private DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub // arg1 = year // arg2 = month // arg3 = day showDate(arg1, arg2+1, arg3); } }; private void showDate(int year, int month, int day) { StringBuilder abbes = new StringBuilder().append(day).append("/") .append(month).append("/").append(year); Bundle bundle = new Bundle(); bundle.putString("edttext", String.valueOf(abbes)); // set Fragmentclass Arguments Fragment5 fragobj = new Fragment5(); fragobj.setArguments(bundle); } 表格和搜索LEFT JOIN条目来找到此问题的一般解决方案。

NULL

答案 1 :(得分:0)

使用内部联接无法解决此问题。内部联接只能对符合条件的行进行,而不是在没有&#39; no&#39;匹配。

您可以对两个表执行左外连接并在用户名上进行匹配,然后在WHERE子句中筛选出具有n.username的NULL值的结果。

SELECT m.username FROM wp_members m LEFT OUTER JOIN members n ON m.username = n.username AND n.siteid = 1 WHERE n.username IS NULL GROUP BY m.username;

另一种方法是使用带有子查询的NOT IN语句。

SELECT m.username FROM wp_members m WHERE m.username NOT IN (SELECT n.username FROM members n AND n.siteid = 1) GROUP BY m.username;

使用外连接的解决方案会更快,但可能不那么重要。